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
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
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.
nums=list(range(1,101))
for i in nums:
if i%2 ==0:
print(i)