What is a Conditional statement?

Conditional statements in Python allow you to execute code based on certain conditions.

Green Oval Terrazzo Photo Instagram Post.png

1. if Statement

x = 10
if x > 5:
    print("x is greater than 5")

2. elif (Else If) Statement

x = 10
if x > 15:
    print("x is greater than 15")
elif x > 5:
    print("x is greater than 5 but less than or equal to 15")

3. else Statement

x = 3
if x > 5:
    print("x is greater than 5")
else:
    print("x is 5 or less")

cregedg.png

Combining Conditions

You can use and, or, and not to combine multiple conditions.

x = 10
y = 20
if x > 5 and y > 15:
    print("Both conditions are true")

Nested conditional statements