Python Practice › If-Else Programs › Program 21
Positive or negative, even or odd
Write a Python program to check whether a number is positive or negative and even or odd.
Input
26, -7, 0
Output
26 is positive and even -7 is negative and odd 0 is zero and even
Approach
- Decide the sign with if-elif-else.
- Decide parity with a conditional expression.
Solution
for n in [26, -7, 0]: if n > 0: sign = "positive" elif n < 0: sign = "negative" else: sign = "zero" parity = "even" if n % 2 == 0 else "odd" print(f"{n} is {sign} and {parity}")Output
26 is positive and even -7 is negative and odd 0 is zero and even
Line by line
Lines 2-7
Three branches cover positive, negative and zero.
Line 8
A one-line conditional expression picks “even” or “odd”.
Learn the concept
New to this topic? Read the lesson, then come back.