Positive or negative, even or odd

EasyIf-Else Programs · 21 of 64

Write a Python program to check whether a number is positive or negative and even or odd.

Advertisement
Input
26, -7, 0
Output
26 is positive and even
-7 is negative and odd
0 is zero and even

Approach

  1. Decide the sign with if-elif-else.
  2. Decide parity with a conditional expression.

Solution

Python
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”.

Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More if-else programs

← All 64 If-Else Programs