print the numbers from 1 to 100, but only print the even numbers in python

Here's the code to print all even numbers within first 100 numbers:

main.py
for i in range(1, 101):
  if i % 2 == 0:
    print(i)
54 chars
4 lines

Explanation:

  • range(1, 101) generates numbers from 1 to 100 (inclusive).
  • if i % 2 == 0 checks if a number is even or not by dividing it by 2 and checking for remainder. If the remainder is 0, then the number is even.
  • print(i) prints the even number.

gistlibby LogSnag