If Statements
If Statements
Introduction
If statements allow your program to make decisions. Code inside an if block only runs when the condition is True.
Basic If Statement
if condition:
# code to run when condition is True
Example
salary = 75000
if salary > 50000:
print("Senior role")
If / Else
salary = 30000
if salary > 50000:
print("Senior role")
else:
print("Junior role")
If / Elif / Else
Use elif (short for "else if") to check multiple conditions in sequence. Python stops at the first match.
salary = 55000
if salary < 30000:
print("Junior")
elif salary < 60000:
print("Mid-level")
elif salary < 100000:
print("Senior")
else:
print("Principal or above")
Output
Mid-level
Indentation Matters
Python uses indentation (4 spaces) to define blocks. Incorrect indentation will cause an IndentationError.
# Correct
if salary > 50000:
print("High earner")
# Wrong — will raise IndentationError
if salary > 50000:
print("High earner")
Nested If Statements
You can place if statements inside other if statements.
department = "Engineering"
salary = 80000
if department == "Engineering":
if salary > 75000:
print("Senior Engineer")
else:
print("Engineer")
Tip: Deeply nested code is hard to read. If you find yourself going more than two levels deep, consider restructuring using
and/or.
Truthy and Falsy Values
In Python, values other than True and False can be treated as booleans in an if condition.
| Falsy | Truthy |
|---|---|
0 | Any non-zero number |
"" (empty string) | Any non-empty string |
[] (empty list) | Any non-empty list |
None | Any object |
name = ""
if name:
print(f"Hello, {name}")
else:
print("No name provided")
Practice Exercises
- Write an
if/elif/elsestatement that categorises a product price: under10is"Cheap", between10and50is"Moderate", above50is"Expensive". - Ask the user for a number. Print whether it is positive, negative, or zero.
- Check if a
usernamevariable is an empty string and print an appropriate message. - Write an
ifstatement usingandto check if a salary is between40000and90000.