-2

Write a script that prints every even numbers between 1 and 100 inclusive, one per line

nums=list(range(1,100))
for i in nums:
    if i%2 ==0:
        print(i)

result:Python's range is half-open (start included, stop excluded). why and how

Antutu Cat
  • 105
  • 4
  • 4
    Does this answer your question? [Why does range(start, end) not include end?](https://stackoverflow.com/questions/4504662/why-does-rangestart-end-not-include-end) – Michael Szczesny Nov 06 '20 at 12:22

2 Answers2

1

Since the stop value is not included, you can just increment the stop value to include the desired value. So, if you want to include "100", you must provide a stop value of "101".

nums = range(1, 101)

Also, you don't necessarily need list() here.

Melvin Abraham
  • 2,870
  • 5
  • 19
  • 33
  • 1
    You should add that he doesn't need nums he can just have the `range(1,101)` directly in the for loop. – Yovel Nov 06 '20 at 12:27
0
nums=list(range(1,101))
for i in nums:
   if i%2 ==0:
       print(i)
Alok Patel
  • 80
  • 2