Conditional statements allow your program to make decisions based on conditions that are either True or False.
if Statement
age = 18
if age >= 18:
print("You are an adult")
else Statement
else runs when the if condition is False.
age = 16
if age >= 18:
print("You are an adult")
else:
print("You are under 18")
elif Statement
elif means "else if" and allows multiple conditions.
score = 85
if score >= 90:
print("Grade A")
elif score >= 80:
print("Grade B")
elif score >= 70:
print("Grade C")
else:
print("Grade F")
x = 10
if x == 10:
print("x is ten")
age = 20
has_id = True
if age >= 18 and has_id:
print("Entry allowed")
else:
print("Entry denied")
Python uses indentation to define blocks of code. Incorrect indentation will cause errors.
= instead of ==: