Lesson 3 – If Statements (Making Decisions)
In this lesson you will:
- Explain what an if statement does.
- Use comparison operators (==, !=, <, >, <=, >=).
- Write simple if and if…else statements with correct indentation.
- Use elif for more than two choices (optional/extension).
- Read and predict simple decision-making programs.
By the end you should be able to:
- Explain what an if statement does.
- Use comparison operators (==, !=, <, >, <=, >=).
- Write simple if and if…else statements with correct indentation.
- Use elif for more than two choices (optional/extension).
- Read and predict simple decision-making programs.
1. Quick recap
From Lesson 2, remember:
- Variables store values.
- We can read input using
input()and convert tointorfloat. - We can do basic arithmetic.
2. What is an if statement?
An if statement lets Python choose whether to run a block of code.
How it works:
- Python checks a condition.
- If the condition is
True→ run the indented block. - If the condition is
False→ skip the block.
Example 1 – Simple if
age = int(input("Age: "))
if age >= 18:
print("You are an adult.")
Key idea: If age is 18 or more, the message prints; otherwise nothing happens.
Try it yourself
Run it several times with ages below and above 18. What happens if you type 18? What about 17?
Output
Example Solution
age = int(input("Age: "))
if age >= 18:
print("You are an adult.")
print("You can vote!")
3. Comparison operators
Comparison operators let you form conditions. Each comparison gives either True or False.
| Operator | Meaning |
|---|---|
== |
equal to |
!= |
not equal to |
< |
less than |
> |
greater than |
<= |
less than or equal to |
>= |
greater than or equal to |
Example 2 – Check a password (very simple)
password = input("Enter password: ")
if password == "python123":
print("Access granted")
Try it yourself
Run it and try typing the correct password and an incorrect one. Then change the password in the code to something else.
Output
Example Solution
password = input("Enter password: ")
if password == "mypassword":
print("Access granted")
print("Welcome!")
4. Indentation and blocks
Important rules:
- The line after
ifends with a colon:. - The code that belongs to the
ifis indented (4 spaces or one Tab). - All lines with the same indentation are part of the same block.
Example 3 – Indentation matters
score = int(input("Score: "))
if score > 50:
print("Well done!")
print("You passed!")
print("This line always runs.")
What happens:
- If
score > 50→ both "Well done!" and "You passed!" print, then the last line. - If not → only the last line prints.
Try it yourself
Change the score threshold (50) to a different number, or change the comparison operator (e.g., from > to < or >=). Predict what will happen, then run it.
Output
Example Solution
score = int(input("Score: "))
if score > 50:
print("Well done!")
print("You passed!") # Not indented - always runs
print("This line always runs.")
5. if…else – two-way choice
if chooses one branch for True. else lets you say what happens when the condition is False.
Example 4 – Pass/fail
score = int(input("Score: "))
if score >= 50:
print("You passed!")
else:
print("You failed.")
Try it yourself
Change the pass mark to 60. Try different scores and predict the output before running.
Output
Example Solution
score = int(input("Score: "))
if score >= 60:
print("You passed!")
else:
print("You failed.")
Example 5 – Even or odd
This uses % (modulus) from Lesson 2.
number = int(input("Enter a number: "))
if number % 2 == 0:
print("Even")
else:
print("Odd")
Why does number % 2 == 0 mean the number is even?
The modulus operator % gives you the remainder after division. When you divide any number by 2:
- If the remainder is
0→ the number is even (divisible by 2). - If the remainder is
1→ the number is odd (not divisible by 2).
Examples:
0 % 2 = 0→ Even (0 ÷ 2 = 0 with remainder 0)2 % 2 = 0→ Even (2 ÷ 2 = 1 with remainder 0)4 % 2 = 0→ Even (4 ÷ 2 = 2 with remainder 0)10 % 2 = 0→ Even (10 ÷ 2 = 5 with remainder 0)3 % 2 = 1→ Odd (3 ÷ 2 = 1 with remainder 1)7 % 2 = 1→ Odd (7 ÷ 2 = 3 with remainder 1)
Try it yourself
Test several numbers: 0, 1, 2, 7, 10. Predict if each is "Even" or "Odd" before running.
Output
Example Solution
number = int(input("Enter a number: "))
if number % 2 == 0:
print("Even")
print("The number is divisible by 2")
else:
print("Odd")
print("The number is not divisible by 2")
6. elif – more than two choices
Use elif ("else if") for 3+ options.
Example 6 – Grade bands
score = int(input("Score: "))
if score >= 80:
print("Grade A")
elif score >= 60:
print("Grade B")
elif score >= 40:
print("Grade C")
else:
print("Grade D")
Try it yourself
Change the score boundaries or add another grade. Try scores like 39, 40, 59, 60, 79, 80.
Output
Example Solution
score = int(input("Score: "))
if score >= 90:
print("Grade A*")
elif score >= 80:
print("Grade A")
elif score >= 60:
print("Grade B")
elif score >= 40:
print("Grade C")
else:
print("Grade D")
9. Understanding Quiz
Answer all questions below, then click "Check Answers" at the end to see your score.
if age >= 18:
print("You are an adult.")
age = int(input("Age: "))
if age < 13:
print("Child ticket")
else:
print("Full price")
score = int(input("Score: "))
if score >= 80:
print("Grade A")
elif score >= 60:
print("Grade B")
elif score >= 40:
print("Grade C")
else:
print("Grade D")
num = int(input("Number: "))
if num > 5:
print("Big")
else:
print("Small")
score = int(input("Score: "))
if score > 50:
print("Pass")
elif score >= 50:
print("Borderline")
else:
print("Fail")
x = int(input("x: "))
if x % 2 == 0:
print("Even")
if x > 5:
print("Big")
else:
print("Small")
n = int(input("n: "))
if n >= 5:
print("A")
if n == 5:
print("B")
else:
print("C")
print("Done")
8. Coding tasks – If statements practice
Complete all the tasks below. Do one task at a time, run your code, and check the output.
-
Task 1 – Temperature checker
Write a program that:- Asks for temp (integer).
- If temp >= 20, prints
Nice and warm. - Else prints
A bit cold.
-
Task 2 – Login check
Write a program that:- Asks for username (string).
- If username == "admin", prints
Welcome, admin. - Else prints
Access denied.
-
Task 3 – Exam result
Write a program that:- Asks for mark out of 100 (integer).
- If mark >= 80, prints
Distinction. - Elif mark >= 50, prints
Pass. - Else prints
Fail.
-
Task 4 – Bus fare
Write a program that:- Asks for age (integer).
- If age < 11, prints
Free travel. - Elif age <= 18, prints
Child fare. - Else prints
Adult fare.
-
Task 5 – Password length checker
Write a program that:- Asks for password (string).
- If the password has 8 or more characters, prints
Strong enough. - Else prints
Too short.
Hint:
len(password)says how many characters were typed. -
Task 6 – Even or odd
Write a program that:- Asks for number (integer).
- If the number is even, prints
Even. - Else prints
Odd.
-
Task 7 – Times table checker
Write a program that:- Asks for number (integer).
- If the number is in the 5 times table (divisible by 5), prints
In the 5 times table. - Else prints
Not in the 5 times table.
-
Task 8 – Guess the number
Write a program that:- Sets
secret = 7. - Asks the user for a guess (integer).
- If the guess is correct, prints
Correct!. - Else prints
Wrong!.
- Sets
Answer all the above tasks here
Use the editor below to complete the coding tasks above.