Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Print All Even Numbers Between 1 and 100

Generate even numbers from two through one hundred in Python. Students use a range step of two and learn why the stop must be beyond the final desired value.

BeginnerPhase 2 - Level 1: Basic LoopingExample 2 of 14
print-all-even-numbers-between-1-and-100.py
1# Print even numbers from 1 to 100
2for i in range(2, 101, 2):
3 print(i)

Output

...

The sample abbreviates a longer result. The program prints the intervening values; it does not print these dots.

In this one

  • Generate only even terms
  • Use a nonunit step
  • Include a desired endpoint

How it works

  1. 1

    Start the range at two.

  2. 2

    Advance by two while staying below 101.

  3. 3

    Print every supplied even value.

Edge cases

Is zero included?

Is zero included?

No. The sequence starts at two.

Is one hundred included?

Is one hundred included?

Yes. It is reached before the excluded stop 101.

What's going on

Starting at two and advancing by two visits only even numbers. The loop does not generate odds and filter them afterward.

range(2, 101, 2) includes one hundred but excludes the stop 101. Each print writes one selected value on a new line. There are fifty iterations, so changing the step affects both the values and the number of printed lines.

Is zero included? No. The sequence starts at two.

Is one hundred included? Yes. It is reached before the excluded stop 101.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

What is the step?

Pick an answer to check it.

Question 2

Is zero included?

Pick an answer to check it.

Questions people ask

Is zero included?

No. The sequence starts at two.

Is one hundred included?

Yes. It is reached before the excluded stop 101.

What is the step?

2.