Skip to content

script_5.py - Print Even Numbers from 0 to 20

Code

Python
#!/usr/bin/env python3
"""Print even numbers from 0 to 20"""

for i in range(0, 21, 2):
    print(i)

Explanation

Line 4: for i in range(0, 21, 2): - range() with three arguments: start, stop, step - 0 is start value (inclusive) - 21 is stop value (exclusive, so stops at 20) - 2 is step size (increment between values) - Generates: 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 - Step parameter lets you skip values - More efficient than checking if i % 2 == 0 inside loop - i takes only even values, no filtering needed

Line 5: print(i) - Simple print, no logic needed - Executes 11 times (0, 2, 4... 20)