Lesson 18/48 ยท ๐ Making Decisions
๐ Making DecisionsLesson 18/48
Phase 2 ยท Making Decisions18 min
Logical Operators
Combine conditions with and, or, not, the glue of real decisions
You can compare two values with
There are three: and, or, not.
==, <, > etc. But what if you need to check *two conditions at once*? That's where logical operators come in.There are three: and, or, not.
and, or, notpython
age = 25
has_ticket = True
# and, both must be True
if age >= 18 and has_ticket:
print("You can enter")
# or, at least one must be True
is_admin = False
is_vip = True
if is_admin or is_vip:
print("Access granted")
# not, flips True to False and vice versa
is_banned = False
if not is_banned:
print("Welcome!")Truth tables, exactly when each returns True:
and, True only when BOTH sides are True:True and True โ True True and False โ False False and True โ False False and False โ False
or, True when AT LEAST ONE side is True:True or True โ True True or False โ True False or True โ True False or False โ False
not, flips the boolean:not True โ False not False โ True
and, True only when BOTH sides are True:
or, True when AT LEAST ONE side is True:
not, flips the boolean:
๐คQuick Check
What does True and False or True evaluate to?
Short-circuit evaluation, Python is lazy (in a good way)
With and: if the left side is False, Python doesn't even check the right side (can't be True anyway).
With or: if the left side is True, Python skips the right side (already True).
This matters when the right side could cause an error:
With and: if the left side is False, Python doesn't even check the right side (can't be True anyway).
With or: if the left side is True, Python skips the right side (already True).
This matters when the right side could cause an error:
Short-circuit in actionpython
# Safe, checks length first before accessing index
text = ""
if len(text) > 0 and text[0] == "H":
print("Starts with H")
else:
print("Empty or doesn't start with H")
# Combining comparisons
score = 75
if 60 <= score <= 100: # Python allows chaining!
print("Pass")Common mistake: Don't write
if x == 1 or 2:, this always evaluates to True because 2 is truthy. Write if x == 1 or x == 2: instead.๐คQuick Check
Which correctly checks if a number is between 1 and 10 (inclusive)?
Practice Exercises
0/3 solvedExercise 1 of 3easy
โฑ 00:00Age & ID Check
A club requires guests to be 18+ AND have a valid ID.
Given: age = 20, has_id = TruePrint "Allowed" if both conditions are met, otherwise print "Denied".
Given: age = 20, has_id = TruePrint "Allowed" if both conditions are met, otherwise print "Denied".
solution.py
1 / 3
Exercise 2 of 3easy
โฑ 00:00Login Validator
A login fails if the username is empty OR the password is shorter than 8 characters.
Given: username = "alice", password = "abc"Print "Login failed: weak password" if the password < 8 chars, "Login failed: no username" if username is empty, otherwise "Login OK".
Given: username = "alice", password = "abc"Print "Login failed: weak password" if the password < 8 chars, "Login failed: no username" if username is empty, otherwise "Login OK".
solution.py
2 / 3
Exercise 3 of 3easyTrace
โฑ 00:00Fix the Logic Bug
The code below should print "Discount applied" if the user is a student OR a senior (age 65+). But it has a logic bug, fix it.
solution.py
3 / 3
Solve all 3 exercises to unlock completion