Print Even Numbers from 1 to 50 in Python

Printing even numbers is a common beginner exercise that helps understand loops and conditions.

In this tutorial, we will explore multiple ways to print even numbers from 1 to 50.

Concept Overview

Even numbers are divisible by 2 without a remainder.

We can use loops and conditions or step values in range() to generate them.

Method 1: Using Step in range()

Python
for i in range(2, 51, 2):
    print(i, end=", ")

This method directly generates even numbers using step value 2.

Method 2: Using Condition

Python
for i in range(1, 51):
    if i % 2 == 0:
        print(i, end=", ")

This method checks each number to see if it is even.

Method 3: Using List Comprehension

Python
print(", ".join(str(i) for i in range(2, 51, 2)))

Output

TEXT
2, 4, 6, 8, 10, ... , 50

Detailed Explanation

range(start, stop, step) allows control over number generation.

Using step=2 skips odd numbers automatically.

Modulo operator (%) helps check divisibility.

Applications

Used in mathematical computations.

Helpful in filtering datasets.

Advantages

Efficient and easy to implement.

Multiple approaches available.

Limitations

Conditional method is slightly slower due to extra checks.

Improvements You Can Make

Print odd numbers instead.

Store results in a list instead of printing.

Extend range dynamically using user input.

Understanding number filtering is essential for mastering Python loops and logic.