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")   # โœ“ prints
Boolean operators:
  • and โ†’ True only when BOTH sides are True
  • or โ†’ True when EITHER side is True
  • not โ†’ reverses True โ†” False
  • Truthiness, 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 check

    Practice Exercises

    0/3 solved
    Exercise 1 of 3easy
    โฑ 00:00

    Can 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:00

    Strong Password

    Write a function 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 letter
    Useful 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:00

    Access Control

    Write a function 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