Making Decisions in Python
Learn how to make your programs respond to different situations using if, elif, and else statements along with comparison operators.
The if Statement
An if statement lets your program make decisions. It checks whether a condition is True or False, and runs different code depending on the result.
age = 16
if age >= 18:
print("You can vote!")
else:
print("You are not old enough to vote yet.")
# Output: You are not old enough to vote yet.
Notice the colon : at the end of the if and else lines, and the indentation (4 spaces) before the code inside each block. Python uses indentation to know which code belongs to which block.
Comparison Operators and elif
Python uses comparison operators to compare values. When you have more than two possible outcomes, use elif (short for "else if") to check additional conditions.
==
Equal to
!=
Not equal to
>
Greater than
<
Less than
>=
Greater or equal
<=
Less or equal
# Grading system using if/elif/else
mark = 75
if mark >= 90:
print("Grade: A - Outstanding!")
elif mark >= 80:
print("Grade: B - Great work!")
elif mark >= 70:
print("Grade: C - Good effort!")
elif mark >= 60:
print("Grade: D - Keep trying!")
else:
print("Grade: F - See your teacher for help.")
# Output: Grade: C - Good effort!
Boolean Logic: and, or, not
You can combine conditions using logical operators. These let you check multiple things at once.
age = 15
has_permission = True
# 'and' - both conditions must be True
if age >= 13 and has_permission:
print("You can create an account!")
# 'or' - at least one condition must be True
day = "Saturday"
if day == "Saturday" or day == "Sunday":
print("It's the weekend!")
# 'not' - reverses a boolean value
is_raining = False
if not is_raining:
print("Let's go outside!")
and requires both conditions to be True. or requires at least one to be True. not flips True to False and False to True.
Key Vocabulary
Condition
An expression that evaluates to True or False, used to control program flow.
Indentation
The spaces at the start of a line that tell Python which code belongs inside an if/elif/else block.
elif
Short for "else if". Checks an additional condition when the previous if/elif was False.
Boolean Logic
Combining conditions with and, or, and not to create complex decisions.
Worked Examples
Write a program that checks if a number is positive, negative, or zero.
number = int(input("Enter a number: "))
if number > 0:
print("The number is positive.")
elif number < 0:
print("The number is negative.")
else:
print("The number is zero.")
Step 1: Get a number from the user and convert it to an integer.
Step 2: Check if it is greater than 0 (positive), less than 0 (negative), or neither (zero).
Key point: The else catches everything that the if and elif did not match.
Check if a student has passed and earned a distinction.
mark = 85
attendance = 92
if mark >= 50 and attendance >= 80:
print("You have passed!")
if mark >= 85:
print("With Distinction!")
else:
print("You did not meet the requirements.")
Step 1: Use and to check both mark and attendance requirements.
Step 2: Use a nested if (an if inside another if) to check for distinction.
Output: You have passed! and With Distinction!
Determine the ticket price based on age.
age = int(input("Enter your age: "))
if age < 5:
price = 0
category = "Free"
elif age <= 17:
price = 10
category = "Child"
elif age <= 64:
price = 20
category = "Adult"
else:
price = 12
category = "Senior"
print(f"{category} ticket: ${price}")
Step 1: Use multiple elif blocks to create different price brackets.
Step 2: The f"..." is an f-string, which lets you embed variables directly inside text.
Key point: Python checks conditions from top to bottom and runs only the first matching block.
Knowledge Check
Select the correct answer for each question. Click "Check Answer" to see if you are right.
Question 1
What will this code print?
x = 10
if x > 5:
print("Big")
else:
print("Small")Question 2
Which operator means "not equal to" in Python?
Question 3
What does True and False evaluate to?
Question 4
What is the purpose of the colon : at the end of an if statement?
Question 5
What will this code print if score = 75?
if score >= 90:
print("A")
elif score >= 70:
print("B")
elif score >= 50:
print("C")
else:
print("D")Key Concepts Summary
- ● if statements let programs make decisions based on conditions that are True or False.
- ● Use elif for additional conditions and else as a catch-all default.
-
●
Comparison operators (
==,!=,>,<,>=,<=) compare values. - ● and, or, and not combine or modify boolean conditions.
- ● Indentation (4 spaces) is essential in Python to define which code belongs to each block.