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 Example

num = -5

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

The code checks the value of the variable num and prints the appropriate message based on its value. If num is less than 0, the message “number is negative” will be printed. If num is equal to 0, the message “0” will be printed. If num is greater than 0, the message “number is positive” will be printed.