Learning Goals
3 minBy the end of this lesson you can:
- Recognise
TrueandFalseas Python's Boolean values. - Predict the result of comparisons between numbers and between strings.
- Store a Boolean in a variable and use it inside an
ifstatement.
Warm-Up
5 minLast lesson we used comparisons like age >= 13 inside if statements. We never paused to ask: what is the result of that comparison? Today, the answer takes centre stage.
Quick predict-the-output puzzle. What does each line print?
print(7 > 3) print(5 == 5) print("nasi" == "Nasi") print(10 != 10)
Show the answers
True— 7 really is greater than 3.True— both sides are 5.False— Python tellsnandNapart.False— 10 is equal to 10, so "not equal" is false.
Every comparison answers with one of two special values: True or False. These are called Booleans, named after George Boole — the 19th-century mathematician who invented this kind of "yes/no logic".
New Concept · The Yes / No Type
12 minPart 1 · True and False are values, just like 7 or "hello"
We already know about a few types of value:
- int — whole numbers like
12. - float — decimal numbers like
3.14. - str — text like
"nasi lemak".
Today we add one more: bool, with exactly two values — True and False. Mind the capital letters; true with a small t is just a name Python doesn't know.
is_open = True is_closed = False print(is_open) print(is_closed)
Part 2 · Every comparison returns a Boolean
The six comparison operators from last lesson don't just sit inside if. Each one produces a Boolean value that you can print, store, or pass around.
print(8 >= 8) print(2 < 1) answer = (5 + 5 == 10) print(answer)
The line answer = (5 + 5 == 10) first computes the comparison (which is True), then stores that Boolean inside a variable called answer.
Part 3 · Storing a comparison in a variable
This is a really useful trick. Once you save a comparison in a Boolean variable, you can give it a name that reads almost like English.
age = 12 is_teen = age >= 13 print(is_teen) if is_teen: print("Welcome to the teens.")
Notice we wrote if is_teen:, not if is_teen == True:. Both work — but the shorter version reads better, because is_teen is already a Boolean.
Part 4 · Comparing strings
== and != work on text as well as numbers. Two strings are equal only if every character matches — Python is fussy about capital letters and even spaces.
print("apple" == "apple") print("apple" == "Apple") print("apple" == "apple ") print("apple" != "orange")
The "string with a sneaky space" bug is the most common reason a user's typed answer "doesn't match". When you compare input() against a fixed string, double-check there's no extra space inside your quotes.
Part 5 · Chained comparisons — a Python superpower
Many languages make you write age >= 13 AND age <= 19 to check "between 13 and 19". Python lets you write it the way you'd say it out loud:
age = 15 is_teen = 13 <= age <= 19 print(is_teen)
Read 13 <= age <= 19 as: "is 13 less than or equal to age, and age less than or equal to 19?". Both checks must be true for the whole chain to be True.
Worked Example · A Quiz Result Card
12 minCikgu Faridah wants a tiny program that turns a quiz score into a friendly result card. We'll compute three Boolean variables first, then print them, then react to them. Save this as result_card.py.
Code
# result_card.py — turn a score into Boolean results name = input("Student name? ") score = int(input("Score out of 100? ")) # 1. Compute Booleans from comparisons passed = score >= 50 excellent = score >= 80 full_marks = score == 100 # 2. Print the raw Booleans, just to see them print() print(name, "scored", score) print("Passed? ", passed) print("Excellent? ", excellent) print("Full marks? ", full_marks) # 3. React using the Booleans print() if full_marks: print("🏅 Perfect score — luar biasa!") elif excellent: print("Amazing work.") elif passed: print("Nice — you cleared the pass mark.") else: print("Keep practising — you'll get there.")
Two sample runs (lines you type are bold):
Output · top of the class
Student name? <strong>Aisyah</strong> Score out of 100? <strong>92</strong> Aisyah scored 92 Passed? True Excellent? True Full marks? False Amazing work.
Output · not quite there
Student name? <strong>Arjun</strong> Score out of 100? <strong>38</strong> Arjun scored 38 Passed? False Excellent? False Full marks? False Keep practising — you'll get there.
What changed from PY-L1-08?
Last lesson the comparison lived inside the if: if score >= 50:. Today we lifted the comparison out and gave it a name: passed = score >= 50. The if reads almost like English now — if passed:. Same logic, much friendlier code.
Try It Yourself
13 minType each task into your editor and run it with two or three different inputs.
Save the result of each comparison in a variable and print it. The first one is done for you — finish the rest:
a = 10 > 5 print(a) b = ??? print(b) c = ??? print(c)
Hint
Any false comparison works for b — try 1 == 2 or "yes" == "no". Any true one works for c.
Ask the user for a username with input(). Save is_admin = (name == "admin") in a Boolean. Then use if is_admin: to print one of two messages — a "Welcome boss!" line if true, otherwise a normal greeting.
Hint
Three lines of code:
name = input("Username? ") is_admin = (name == "admin") if is_admin: print("Welcome, boss!") else: print("Hello,", name)
Remember — the comparison is case-sensitive. "Admin" is not the same as "admin".
Ask the user for a temperature in °C, convert it with int(), and check whether it lands in the "pleasant" band 22 to 28 using a chained comparison. Print the Boolean, then a message.
Hint
Build the chain as is_pleasant = 22 <= temp <= 28. Then if is_pleasant: prints "Lovely weather for a walk."; otherwise print "Best stay indoors.".
Mini-Challenge · Library Card Validator
8 minTan Sri Library in Petaling Jaya needs a tiny check-in helper. Build it.
Your file must:
- Ask for the visitor's
name. - Ask for their
ageand convert withint(). - Ask "Do you have your library card? (yes/no)" and save the answer in a string variable
card_answer. - Compute these Booleans:
has_card— iscard_answerexactly equal to"yes"?is_member_age— is the age between 7 and 80 (inclusive) using a chained comparison?is_kid— is the age less than 13?
- Print all three Booleans on their own lines, prefixed by their names.
- Use
if has_card:to print one of two final messages: "✅ You may enter, {name}." or "✗ Sorry, {name} — please bring your card next time.".
Stretch goal. Add one more line — when is_kid is True, also print "🍭 Pick up a free sticker on your way in!" right after the entry message.
Show one possible solution
# library_validator.py — sweet little gate-keeper # 1. Ask name = input("What's your name? ") age = int(input("How old are you? ")) card_answer = input("Do you have your library card? (yes/no) ") # 2. Booleans has_card = card_answer == "yes" is_member_age = 7 <= age <= 80 is_kid = age < 13 # 3. Show the Booleans print() print("has_card: ", has_card) print("is_member_age: ", is_member_age) print("is_kid: ", is_kid) # 4. Use one Boolean to decide print() if has_card: print("✅ You may enter,", name + ".") else: print("✗ Sorry,", name, "— please bring your card next time.") # 5. Stretch — extra welcome for kids if is_kid: print("🍭 Pick up a free sticker on your way in!")
If the user types Yes with a capital Y, your check returns False — that's the case-sensitivity bug from Part 4. We'll learn how to handle that politely in PY-L1-10.
Recap
3 minEvery comparison in Python returns a Boolean — either True or False. We can store these results in variables, give them readable names, and use them inside if statements to keep code clear. Python's chained comparison (13 <= age <= 19) is a friendly shortcut for "in this range".
Vocabulary Card
- Boolean (bool)
- A type with only two values:
TrueandFalse. Capital first letter. - comparison
- A line like
a == borx < ywhose result is a Boolean. - Boolean variable
- A variable that holds
TrueorFalse. Often named with words likeis_,has_,can_. - chained comparison
- A Python shortcut:
a <= b <= cmeans "b is between a and c".
Homework
4 minCreate a new file called birthday_card.py — a tiny birthday-greeting maker.
Rules:
- Ask for the user's
nameandage(convert withint()). - Set these three Boolean variables, each from a single comparison:
is_kid— true if the age is below 13.is_teen— true if the age is between 13 and 19 (inclusive). Use a chained comparison.is_adult— true if the age is 20 or above.
- Print all three Booleans on their own lines, each prefixed by a label.
- Use
if/elif/elseon the Boolean variables (not on the raw age) to print one tailored birthday message.
Bring birthday_card.py to next lesson — we'll wish each other happy birthday with each other's scripts!
Sample · birthday_card.py
# birthday_card.py — a small birthday greeting maker # 1. Ask name = input("What is your name? ") age = int(input("How old will you be? ")) # 2. Three Booleans, each one comparison is_kid = age < 13 is_teen = 13 <= age <= 19 is_adult = age >= 20 # 3. Show them print() print("is_kid: ", is_kid) print("is_teen: ", is_teen) print("is_adult: ", is_adult) # 4. Pick a tailored greeting print() if is_kid: print("🎈 Happy birthday,", name + "! Enjoy a slice of kek lapis.") elif is_teen: print("🎉 Happy birthday,", name + "! Big teen energy.") else: print("🎂 Wishing you a wonderful birthday,", name + ".")
Your messages and emojis can differ — what matters is that you compute three Booleans first, then branch on those Booleans (not on age directly).