Python program to print even and odd numbers from 1 to 100

In this article you’ll learn:

  • Python program to check whether a number is even or odd
  • Python program to print even and odd numbers from 1 to 100 using various methods.

Prerequisite knowledge:

  • if-else statements in python
  • Loops in python
  • List comprehension in python
  • Basic python statements

Program: Python program to check whether the entered number is even or odd

num = int(input("Enter a number: "))
if num % 2 == 0:
    print("\nEntered number is even")
else:
    print("\nEntered number is odd")

Output:

Enter a number: 543 

Entered number is odd

Now that we’ve learned how to check even/odd in python, we can proceed to the next step.

Python program to print even numbers from 1 to 100

Script #1: This can be done by using only one line of code in Python. Interesting?

Using List comprehension

[print(num) if num%2==0 else False for num in range(1, 101)]

[OR]

[print(num) for num in range(2, 101, 2)]   #using range and step

Using filter and lambda function- This will print a list, use the concept and share your code in the comments.

print(list(filter(lambda num: num%2==0, range(1,101))))

Script #2: Program using multiple lines

for num in range(1,101):
    if(num%2 == 0):
        print(num)

Don’t forget to write code with proper indentation.

Same concepts can be used to print odd numbers from 1 to 100. I’m sharing the program with multiple lines, write one liner code in the comments.

Program: Python program to print odd numbers from 1 to 100

for num in range(1,100):
    if(num%2 == 1):
        print(num)

Super easy, right?

Share your code and in the comments to help others.

Join us on Facebook to get updated on more interesting programming articles and code snippets.

PROgramming Minds

About ᴾᴿᴼᵍʳᵃᵐᵐᵉʳ

Linux and Python enthusiast, in love with open source since 2014, Writer at programming-articles.com, India.

View all posts by ᴾᴿᴼᵍʳᵃᵐᵐᵉʳ →

Leave a Reply

Your email address will not be published. Required fields are marked *