Python Code Example: check if a number is positive or negative

A simple Python program that shows the functionality of an if-else statement.

Code Explanation

LineDescription
1Initializes a variable called num with the value -5
3 – 4If the value of the variable num is less than 0, the string “number is negative” is output
5 – 6If the value of the variable num is 0, the string “0” is output
7 – 8Otherwise the string “number is positiv” is output
num = -5

if num < 0:
    print("number is negative")
elif num == 0:
    print("0")
else:
    print("number is positiv")
Output
number is negative