Easy Tutorial
❮ Python Os Dup2 Python Area Triangle ❯

Python if Statement

Python3 Examples

The following example uses the if...elif...else statement to determine if a number is positive, negative, or zero:

Example (Python 3.0+)

# Filename : test.py
# author by : www.tutorialpro.org

# User input number

num = float(input("Enter a number: "))
if num > 0:
   print("Positive number")
elif num == 0:
   print("Zero")
else:
   print("Negative number")

Executing the above code outputs:

Enter a number: 3
Positive number

We can also use nested if statements to achieve the same:

Example (Python 3.0+)

# Filename : test.py
# author by : www.tutorialpro.org

# Nested if statements

num = float(input("Enter a number: "))
if num >= 0:
   if num == 0:
       print("Zero")
   else:
       print("Positive number")
else:
   print("Negative number")

Executing the above code outputs:

Enter a number: 0
Zero

Python3 Examples

❮ Python Os Dup2 Python Area Triangle ❯