In real life, we make decisions every day: "If it's raining, take an umbrella. Otherwise, wear sunglasses."
In programming, we use selection statements to make decisions. Python gives us if, elif, and else.
if StatementSyntax:
if condition:
# code runs if condition is True
Example:
age = 18
if age >= 18:
print("You are an adult.") # β
This runs
age = 15
if age >= 18:
print("This won't print") # β Condition is False
π Notice the colon : at the end of the if line, and the indentation (4 spaces) before print. Python uses indentation to know which code belongs to the if.
if statement that prints "You passed!" if score is 50 or more.if statement that prints "Even number" if a number is divisible by 2.if...elseSyntax:
if condition:
# runs if True
else:
# runs if False
Example:
score = 45
if score >= 50:
print("Pass β
")
else:
print("Fail β β better luck next time!")
if...elif...else ChainSyntax:
if condition1:
# runs if condition1 is True
elif condition2:
# runs if condition1 is False and condition2 is True
else:
# runs if none of the above are True
Example β grade calculator:
mark = 75
if mark >= 90:
grade = "A*"
elif mark >= 80:
grade = "A"
elif mark >= 70:
grade = "B"
elif mark >= 60:
grade = "C"
else:
grade = "D"
print(f"Your grade is {grade}") # B
True, it runs that block and skips the rest. Order matters!
and, or, notSyntax:
if condition1 and condition2: # both True
if condition1 or condition2: # at least one True
if not condition: # the opposite
Examples:
age = 20
has_ticket = True
if age >= 18 and has_ticket:
print("Welcome to the show! π")
if age < 12 or age > 65:
print("You get a discount! π")
if not has_ticket:
print("Please buy a ticket first.")
x is between 10 and 20 (inclusive).| Python | Meaning | Pseudocode |
|---|---|---|
== | Equal to | = |
!= | Not equal to | <> |
< | Less than | < |
<= | Less/equal | <= |
> | Greater than | > |
>= | Greater/equal | >= |
Syntax β an if inside an if:
if outer_condition:
if inner_condition:
# both are True
Example:
age = 20
has_id = True
if age >= 18:
if has_id:
print("Welcome! π")
else:
print("Need ID first.")
else:
print("Too young.")
π Each level of nesting adds 4 more spaces. Keep it to 3 levels max in exams!
Writing code is only half the story. You also need to check inputs are correct and test your program thoroughly. Let's look at three key IGCSE topics.
Validation checks if data is reasonable and complete before the program uses it.
1. Range check β is the value within limits?
age = int(input("Enter age (0-120): "))
if age < 0 or age > 120:
print("Invalid age! β")
2. Type check β is it the right kind of data?
try:
score = int(input("Enter score: "))
print(f"Valid score: {score}")
except ValueError:
print("That's not a number! β")
3. Length check β is it the right length?
password = input("Create password (min 8 chars): ")
if len(password) < 8:
print("Too short! β")
4. Presence check β was something entered?
name = input("Enter your name: ")
if name == "":
print("Name cannot be empty! β")
5. Format check β does it match a pattern?
email = input("Enter email: ")
if "@" not in email or "." not in email:
print("Invalid email format! β")
When testing, you need different kinds of test data:
| Type | Meaning | Example (age 0-120) |
|---|---|---|
| Normal | Typical, expected values | 25, 0, 120 |
| Abnormal | Invalid values that should be rejected | -5, 200, "hello" |
| Boundary | At the edges (includes just outside) | -1, 0, 120, 121 |
| Extreme | Largest/smallest acceptable | 0, 120 |
A trace table lets you step through an algorithm on paper, tracking how variable values change.
Example β find the highest score:
scores = [3, 7, 2]
highest = scores[0]
for i in range(1, len(scores)):
if scores[i] > highest:
highest = scores[i]
print(highest)
| i | scores[i] | > highest? | highest | Output |
|---|---|---|---|---|
| β | β | β | 3 | |
| 1 | 7 | True | 7 | |
| 2 | 2 | False | 7 | |
| β | β | β | 7 | 7 |