Learning Goals
3 minBy the end of this lesson you can:
- Combine two Booleans with
and— both sides must be true. - Combine two Booleans with
or— at least one side must be true. - Flip a Boolean with
not— turnsTrueintoFalseand vice versa.
Warm-Up
5 minLast lesson, every Boolean came from one comparison. Real life is bigger than that. A trip to the mamak only happens if you have money and time. A wet day means staying in or getting soaked. Today's lesson is about the small words that join Booleans together.
Quick puzzle. What does each line print?
print(True and True) print(True and False) print(False or True) print(not True)
Show the answers
True— both are true.False—andneeds both sides true.True—oronly needs one side true.False—notflips the value.
Python's logical operators are real English words — and, or, not — not symbols like && or ||. Read your code out loud and it usually says what it does.
New Concept · Glue Words for Booleans
12 minPart 1 · and — both sides must be true
Use and when every condition has to be true. If either side is false, the whole expression is false.
has_money = True has_time = False print(has_money and has_time) age = 14 in_form_1_to_5 = 13 <= age <= 17 brought_slip = True can_join_trip = in_form_1_to_5 and brought_slip print(can_join_trip)
Read in_form_1_to_5 and brought_slip as: "in form 1 to 5 and brought slip". Plain English.
Part 2 · or — at least one side must be true
or is the softer cousin. It's true if either side is true. Only "both false" makes the whole thing false.
is_raining = False is_hot = True stay_inside = is_raining or is_hot print(stay_inside) print(False or False) print(True or False)
When you ask "tea or coffee?" at the mamak, you mean one of them, not both. In Python, or is happy with one side or both sides being true. The technical name for this is inclusive or.
Part 3 · not — flip the Boolean
not works on a single Boolean (no glue between two things). It flips True into False and the other way round.
is_open = True print(not is_open) has_umbrella = False if not has_umbrella: print("Grab a brolly before you go!")
if not has_umbrella: reads exactly like English — "if you don't have an umbrella". That's the most common place you'll see not in real code.
Part 4 · Combining all three
You can chain operators together to ask richer questions.
age = 15 has_consent = True is_school_day = False # Trip is on if you have consent AND it's not a school day can_go = has_consent and not is_school_day print(can_go) # Free entry if very young OR very old age2 = 70 is_free = age2 < 5 or age2 >= 65 print(is_free)
Python checks not first, then and, then or — like maths does × before +. Don't memorise this — when in doubt, add parentheses. (is_member or is_staff) and brought_pass is always clearer than the same line without brackets.
Part 5 · A friendlier "yes" check
Last lesson we saw a case-sensitivity bug — "Yes" with a capital Y didn't match "yes". With or, the fix is one line:
answer = input("Continue? (yes/no) ") said_yes = answer == "yes" or answer == "Yes" or answer == "YES" print(said_yes)
Now any of the three spellings counts as a yes. We'll learn an even tidier way (answer.lower()) in PY-L1-12 — for today, or is plenty.
Worked Example · After-School Lab Access
12 minThe school computer lab in SMK Bukit Bintang has rules. It only opens on Monday, Wednesday and Friday. Students need to be in Form 1 or above (age 13+) or have a signed parent's slip if younger. We'll write a tiny gatekeeper. Save this as lab_access.py.
Code
# lab_access.py — after-school computer lab gatekeeper # 1. Ask name = input("What's your name? ") age = int(input("How old are you? ")) consent_ans = input("Parent's permission? (yes/no) ") open_ans = input("Is today a lab day (Mon/Wed/Fri)? (yes/no) ") # 2. Booleans from the inputs has_consent = consent_ans == "yes" is_open_day = open_ans == "yes" old_enough = age >= 13 # 3. Combine with and / or can_enter = is_open_day and (old_enough or has_consent) # 4. Show and react print() print("old_enough: ", old_enough) print("has_consent: ", has_consent) print("is_open_day: ", is_open_day) print("can_enter: ", can_enter) print() if can_enter: print("✅ Welcome,", name + "! Lab is open until 5 pm.") else: print("✗ Sorry,", name + " — try again on a lab day.") # 5. The "not" — a gentle reminder if not has_consent: print("Tip: bring a signed permission slip next time.")
Two sample runs (lines you type are bold):
Output · young student with permission slip on a lab day
What's your name? <strong>Priya</strong> How old are you? <strong>11</strong> Parent's permission? (yes/no) <strong>yes</strong> Is today a lab day (Mon/Wed/Fri)? (yes/no) <strong>yes</strong> old_enough: False has_consent: True is_open_day: True can_enter: True ✅ Welcome, Priya! Lab is open until 5 pm.
Output · older student, lab is closed today
What's your name? <strong>Wei Jie</strong> How old are you? <strong>15</strong> Parent's permission? (yes/no) <strong>no</strong> Is today a lab day (Mon/Wed/Fri)? (yes/no) <strong>no</strong> old_enough: True has_consent: False is_open_day: False can_enter: False ✗ Sorry, Wei Jie — try again on a lab day. Tip: bring a signed permission slip next time.
Reading the big line slowly
can_enter = is_open_day and (old_enough or has_consent)
Inside the brackets: old enough or has consent. That's the "either of these is fine" rule for the student. Outside the brackets: is open day and (whatever was inside). The lab must be open and the student must qualify. Both halves are necessary.
Try It Yourself
13 minRun each task with at least two different inputs to see both branches.
Without running anything, write down what each line will print. Then run them to check.
print(True and True) print(True and False) print(False or False) print(True or False) print(not False) print(not (5 > 2))
Show the answers
True, False, False, True, True, False.
Ask for a username and a password. The login succeeds only if the username equals "aisyah" and the password equals "chocolate". Print "Login OK" or "Login failed".
Hint
username = input("Username? ") password = input("Password? ") ok = username == "aisyah" and password == "chocolate" if ok: print("Login OK") else: print("Login failed")
Ask for the user's age. Print "Free entry" if they're under 5 or 65 and over. Otherwise print "Pay full price".
Hint
Build the Boolean first, then branch:
age = int(input("Age? ")) is_free = age < 5 or age >= 65 if is_free: print("Free entry") else: print("Pay full price")
Mini-Challenge · Pasar Malam Lucky Draw
8 minPasar Malam SS2 is running a lucky-draw promotion. Build a tiny eligibility checker.
Your file must:
- Ask for the shopper's
name. - Ask how much they spent (
spend) — usefloat(). - Ask "Are you a regular? (yes/no)" and save the answer.
- Ask "Did you bring a friend? (yes/no)" and save the answer.
- Compute these Booleans:
big_spender— spent at least RM 30.is_regular— they typed"yes"(any of"yes","Yes","YES").brought_friend— they typed any spelling of"yes".eligible—big_spenderand (is_regularorbrought_friend).missed_out— noteligible.
- Print every Boolean on its own line, prefixed by its name.
- If
eligibleis true, print "🎟 You're in the lucky draw, {name}!". Otherwise print "Sayang — not quite. Come back next week, {name}.".
Stretch goal. Add a Boolean jackpot — true only when the shopper spent at least RM 100 and is a regular. If jackpot is true, print one extra line: "🏆 You also qualified for the grand prize!".
Show one possible solution
# pasar_malam_draw.py — lucky draw eligibility # 1. Ask name = input("Your name? ") spend = float(input("How much did you spend (RM)? ")) reg_ans = input("Are you a regular? (yes/no) ") friend_ans = input("Did you bring a friend? (yes/no) ") # 2. Booleans big_spender = spend >= 30 is_regular = reg_ans == "yes" or reg_ans == "Yes" or reg_ans == "YES" brought_friend = friend_ans == "yes" or friend_ans == "Yes" or friend_ans == "YES" eligible = big_spender and (is_regular or brought_friend) missed_out = not eligible # 3. Show them print() print("big_spender: ", big_spender) print("is_regular: ", is_regular) print("brought_friend: ", brought_friend) print("eligible: ", eligible) print("missed_out: ", missed_out) # 4. Final message print() if eligible: print("🎟 You're in the lucky draw,", name + "!") else: print("Sayang — not quite. Come back next week,", name + ".") # 5. Stretch — jackpot jackpot = spend >= 100 and is_regular if jackpot: print("🏆 You also qualified for the grand prize!")
If your eligible line drops the brackets — big_spender and is_regular or brought_friend — Python's precedence still groups it as (big_spender and is_regular) or brought_friend, which is a slightly different rule! Brackets keep your intent clear.
Recap
3 minThree little words — and, or, not — turn one-condition checks into the kind of rich logic real software relies on. Group with brackets when you mix them, and remember or in Python is happy with one or both sides being true.
Vocabulary Card
- and
- True only when both sides are true.
- or
- True when at least one side is true. Inclusive — both being true also counts.
- not
- Flips a Boolean.
not TrueisFalse;not FalseisTrue. - logical operator
- The family name for
and,orandnot— they all work on Booleans.
Homework
4 minCreate a new file called chess_club.py — sign-up checker for the school chess club.
Rules:
- Ask for
name,age(convert withint()), andperm_ans("Do you have parent permission? (yes/no)"). - Compute these Booleans (each one line):
right_age—13 <= age <= 17(a chained comparison from PY-L1-09).has_permission—perm_ans == "yes".old_enough_alone—age >= 16(over-16s don't need permission).can_join—right_age and (has_permission or old_enough_alone).blocked—not can_join.
- Print every Boolean on its own line, prefixed by its name.
- Use
if can_join:to print either "♟ Welcome to the club, {name}!" or "Sorry {name}, ask a parent and try again.".
Bring chess_club.py next lesson — we'll test each other's rules with edge-case ages (12, 15 with no permission, 17 with permission…).
Sample · chess_club.py
# chess_club.py — sign-up gatekeeper for the school chess club # 1. Ask name = input("Your name? ") age = int(input("How old are you? ")) perm_ans = input("Do you have parent permission? (yes/no) ") # 2. Booleans right_age = 13 <= age <= 17 has_permission = perm_ans == "yes" old_enough_alone = age >= 16 can_join = right_age and (has_permission or old_enough_alone) blocked = not can_join # 3. Show the Booleans print() print("right_age: ", right_age) print("has_permission: ", has_permission) print("old_enough_alone: ", old_enough_alone) print("can_join: ", can_join) print("blocked: ", blocked) # 4. Friendly final message print() if can_join: print("♟ Welcome to the club,", name + "!") else: print("Sorry", name + ", ask a parent and try again.")
Your variable names and message wording can vary — what matters is that can_join uses and with brackets around the or, and that the final if branches on can_join (not on age directly).