← Semester 2

IT · Sem 2 · Chapter 2

Done
IT · Semester 2 · Chapter 2 · Grade 8 (M.2)

Problem Solving with Python 🐍

Boolean operators, while loops, if/elif/else, and functions — building on last year's if/if-else into real decision-making and reusable code.

🕶️ with Guru Jazzy

🎯 What you'll unlock this lesson

  • Use comparison and boolean operators to write conditions
  • Repeat code with while loops
  • Branch between more than two outcomes with if / elif / else
  • Write and call your own functions with parameters and return values

💡 Comparison and boolean operators 2.2

Last year's Python used if / if-else with simple conditions. This year, conditions get richer with comparison operators and boolean operators that combine them.

Comparison operators

OperatorMeaning
==equal to
!=not equal to
<less than
<=less than or equal to
>greater than
>=greater than or equal to

Boolean operators

OperatorMeaning
andtrue only if both sides are true
ortrue if at least one side is true
notflips true ↔ false
KEY TERM · Boolean expression — an expression that evaluates to exactly True or False.

🎢 Worked example: ride safety check

A theme park ride requires a rider to be taller than 115cm, or older than 3 and riding with an adult. As a boolean check for "needs an adult":

needs_adult = height <= 115 and age <= 3
heightageheight <= 115age <= 3needs_adult
1102TrueTrueTrue
1105TrueFalseFalse
1202FalseTrueFalse
1205FalseFalseFalse

and only gives True when both sides are True — that's why row 2 (tall enough already) is False.

🃏 Key-term flashcards tap to flip

🔁 The while loop 2.3

A while loop repeats a block of code as long as its condition stays True. It's the tool for "keep asking until the answer is valid."

📄 Syntax
while condition:
    # repeated while condition is True
    statement(s)

🎮 Interactive Step-Through: validating input signature move

This program keeps asking for a number until it gets one from 1–10. Hit Step to watch the variables and console update, line by line.

Variables

Console output

🧪 TASK — What if the user enters 15, then -2, then 7?

Run the simulator above — it feeds exactly those three attempts in. Both 15 and -2 fail the 1 <= n <= 10 check, so the loop keeps going; 7 passes and the loop ends.

🔀 if / elif / else 2.4

elif lets you check more than two possibilities in order — Python checks each condition top to bottom and runs the first one that's True, then skips the rest.

📄 Syntax
if condition1:
    statement(s)
elif condition2:
    statement(s)
else:
    statement(s)

📐 Worked example 2.6: area of a shape

One function, three shapes, branching on a shape string:

import math

if shape == "circle":
    area = math.pi * radius ** 2
elif shape == "triangle":
    area = 0.5 * base * height
elif shape == "rectangle":
    area = width * height
else:
    area = None  # unrecognised shape

Notice each elif is only checked if every condition above it was False.

🗣️ Activity 2.1 — trace it yourself first

For shape = "triangle", base = 6, height = 4: which branch runs, and what's the area?

Reveal answer

shape == "circle" → False, skip. shape == "triangle" → True → area = 0.5 * 6 * 4 = 12.0. The rectangle branch is never even checked.

🧰 Functions 2.5

A function packages up a block of code under a name, so it can be reused without retyping it. def defines one; calling it by name runs it.

📄 Syntax
def function_name(parameter1, parameter2):
    statement(s)
    return value
KEY TERM · Parameter vs. argument — a parameter is the named placeholder in the function's definition; an argument is the actual value passed in when the function is called.

Positional arguments

Matched to parameters by order.

classify(110, 5)

Keyword arguments

Matched to parameters by name — order doesn't matter.

classify(age=5, height=110)

🎮 Interactive Step-Through: a function that combines it all worked example 2.7

A function using boolean operators and if/elif/else, called with real arguments. Step through to see the parameters fill in, the branch get chosen, and the return value come back.

Variables

Console output

🧠 Mini-quiz — no cap, prove it

📝 Exam-style questions try, then peek

1 · Evaluate: (5 > 3) and (2 == 2)

5 > 3 is True, 2 == 2 is True. True and TrueTrue.

2 · How many times does this loop run?
n = 0
while n < 3:
    print(n)
    n = n + 1

Prints 0, 1, 2 — 3 times. When n becomes 3, n < 3 is False and the loop stops.

3 · What does classify_rider(90, 2) return, using the function from the Functions tab?

height <= 115 and age <= 3True and True → True → the first branch runs → returns "Not allowed on this ride".

Guru Jazzy 🕶️ · Sem 2 · Chapter 2 · Problem Solving with Python