Lesson 17/48 ยท ๐ Making Decisions
๐ Making DecisionsLesson 17/48
Phase 2 ยท Making Decisions20 min
Boolean Logic
Combine conditions with and, or, not, and master Python truthiness
You've used
if with simple conditions like x > 5. But real programs need to check multiple things at once, "is the user logged in AND does she have a premium account?" That's where boolean logic comes in.and, or, not in actionpython
age = 20
has_ticket = True
# AND: both must be True
if age >= 18 and has_ticket:
print("You can enter") # โ prints
# OR: at least one must be True
is_member = False
has_guest_pass = True
if is_member or has_guest_pass:
print("Welcome!") # โ prints
# NOT: flips True โ False, False โ True
is_closed = False
if not is_closed:
print("Store is open") # โ printsBoolean operators:
and โ True only when BOTH sides are Trueor โ True when EITHER side is Truenot โ reverses True โ FalseTruthiness, Python treats some values as "false-like" even when they aren't literally
False. This lets you write cleaner conditions.Falsy values: 0, "", [], Nonepython
# All of these behave like False in an if statement:
if 0: print("won't run")
if "": print("won't run")
if []: print("won't run")
if None: print("won't run")
# All of these behave like True:
if 42: print("runs!")
if "hello": print("runs!")
if [1, 2]: print("runs!")
# Practical: check if a string is empty
name = ""
if not name:
print("Please enter your name") # prints!if username: and if username != "": are equivalent in Python. The first is considered more "Pythonic", shorter and just as clear.Short-circuit evaluation, Python stops evaluating as soon as the result is certain. In
A and B, if A is False, Python never checks B. In A or B, if A is True, B is never checked. This matters when B might crash.Short-circuit safetypython
users = []
# Without short-circuit guard, crashes if list is empty:
# if users[0] == "admin": โ IndexError!
# Safe version using short-circuit:
if users and users[0] == "admin":
print("Admin found")
# If users is empty (falsy), Python skips the second checkPractice Exercises
0/3 solvedExercise 1 of 3easy
โฑ 00:00Can They Enter?
Write a function
can_enter(age, has_ticket) that returns True only if the person is 18 or older AND has a ticket.solution.py
1 / 3
Exercise 2 of 3easy
โฑ 00:00Strong Password
Write a function
Useful built-ins:
is_strong(password) that returns True if the password meets ALL three rules:- At least 8 characters long- Contains at least one digit- Contains at least one uppercase letterUseful built-ins:
any(c.isdigit() for c in s) checks for digits, any(c.isupper() for c in s) checks for uppercase.solution.py
2 / 3
Exercise 3 of 3medium
โฑ 00:00Access Control
Write a function
All other combinations return
can_access(role, is_active, resource) that returns True if:- The role is "admin" (admins can access anything), OR- The role is "user" AND is_active is True AND resource is "public"All other combinations return
False.solution.py
3 / 3
Solve all 3 exercises to unlock completion