else
?The else
keyword lets you run code when the if
condition is false.
Example:
age = 16
if age >= 18:
print("Access granted")
else:
print("Access denied")
In this example, since age
is less than 18, Python runs the else
block instead.
elif
?elif
stands for "else if", and it lets you check multiple conditions.
Example:
score = 75
if score >= 90:
print("Excellent")
elif score >= 70:
print("Good")
else:
print("Keep trying")
Python checks each condition from top to bottom and runs the first one that is true.