Skip to content

Python, how to check if a number is odd or even

New Course Coming Soon:

Get Really Good at Git

A number is even when divided by 2 the remainder is 0. Think 2, 4, 10, 200.000.

Odd numbers generate a remainder of 1: 1, 3, 5, 15…

You can check if a number is even or odd with an if conditional:

num = 3
if (num % 2) == 0:
   print('even')
else:
   print('odd')

If you have an array of numbers and want to get the ones even or odd, you can use filter() with a lambda function:

numbers = [1, 2, 3]

even = filter(lambda n: n % 2 == 0, numbers)
odd = filter(lambda n: n % 2 == 1, numbers)

print(list(even)) # [2]
print(list(odd)) # [1,3]
Are you intimidated by Git? Can’t figure out merge vs rebase? Are you afraid of screwing up something any time you have to do something in Git? Do you rely on ChatGPT or random people’s answer on StackOverflow to fix your problems? Your coworkers are tired of explaining Git to you all the time? Git is something we all need to use, but few of us really master it. I created this course to improve your Git (and GitHub) knowledge at a radical level. A course that helps you feel less frustrated with Git. Launching Summer 2024. Join the waiting list!
→ Get my Python Handbook
→ Get my Python Handbook

Here is how can I help you: