Conditional statements allow your program to make decisions and run different blocks of code depending on whether a condition is true or false.
if: Executes code if the condition isTrue.elif(else if): Checks another condition if the previous one wasFalse.else: Catches everything not caught by preceding conditions.
score = 85
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
else:
print("Grade: C")You can place if statements inside another if statement to build complex decision trees.
x = 41
if x > 10:
print("Above ten,")
if x > 20:
print("and also above 20!")A ternary expression allows you to quickly evaluate a condition and assign a value on a single line.
# Syntax: value_if_true if condition else value_if_false
age = 20
status = "adult" if age >= 18 else "minor"
print(status) # Output: adultIntroduced in Python 3.10, the match statement provides structural pattern matching, which acts like a modern version of switch-case statements in other languages. The underscore _ acts as the wildcard/default case.
status_code = 404
match status_code:
case 200:
print("Success")
case 404:
print("Not Found")
case 500:
print("Server Error")
case _:
print("Unknown Status")In Python, you do not always need to check if x == True: or if len(x) > 0:. Python evaluates empty collections and zero values as False (Falsy) and non-empty values as True (Truthy).
The following values evaluate to Falsy:
NoneFalse0(and0.0,0j)- Empty sequences/mappings:
"",[],(),{},set()
# Common Beginner Mistake:
username = ""
if len(username) == 0:
print("No username entered")
# Idiomatic Python Way:
if not username:
print("No username entered")