Check if a Number is Even or Odd
Use a remainder to classify whole numbers as even or odd. This first Python condition exercise includes zero, negative integers, and the meaning of modulo.
1# Take a number as input2num = int(input("Enter a number: "))34# Check if even or odd5if num % 2 == 0:6 print("Even")7else:8 print("Odd")
Output
Enter a number: 8 Even Enter a number: 7 Odd
In this one
- Recognize integer parity
- Calculate a remainder
- Include zero and negatives
How it works
- 1
Convert the response to int.
- 2
Calculate num % 2.
- 3
Print Even for zero remainder, otherwise Odd.
Edge cases
Is zero even?
Is zero even?
Yes. Zero divided by two has remainder zero.
Does -3 work?
Does -3 work?
Yes. In Python, -3 % 2 is 1, so the result is Odd.
What's going on
Parity means whether a whole number divides evenly by two. int converts the input text, and num % 2 computes the remainder, not the quotient.
A remainder of zero selects Even; otherwise the program selects Odd. For 8, the remainder is zero. For 7, it is one. The code needs only two branches because every integer belongs to exactly one group.
Is zero even? Yes. Zero divided by two has remainder zero.
Does -3 work? Yes. In Python, -3 % 2 is 1, so the result is Odd.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Which operator tests the remainder?
Pick an answer to check it.
Question 2
Is zero even?
Pick an answer to check it.
Questions people ask
Is zero even?
Yes. Zero divided by two has remainder zero.
Does -3 work?
Yes. In Python, -3 % 2 is 1, so the result is Odd.
Which operator tests the remainder?
%.
๐Related Content
- ๐
Phase 1 - Learn Concepts
Review Phase 1 concepts and explanations
- ๐
Phase 1 - Quiz
Test your Phase 1 understanding with quiz questions
- ๐ป
Phase 1 - All Practice Problems
Explore all practice problems for Phase 1
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum