🚦 Making Smarter Decisions
Sometimes, a single check isn't enough. AI often needs to evaluate multiple conditions at once to make the right choice. Should a self-driving car brake? Is an email spam? Access granted?
This module introduces Python's logical operators: `and`, `or`, and `not`. These tools allow us to combine simple True/False checks into complex decision rules, a core part of Logic Logic and breaking down problems Decomposition.
🤝 The `and` Operator: Both Must Be True
Use `and` when you need ALL conditions to be `True` for the overall result to be `True`.
Think of needing a username AND password to log in. If either one is wrong, access is denied.
In Python, `condition1 and condition2` results in `True` only if BOTH `condition1` and `condition2` are individually `True`.
It's like requiring two keys to open a lock. Missing one? It stays locked.
has_ticket = True
is_adult = True
can_enter_movie = has_ticket
and is_adult
print(f"Can enter? {can_enter_movie
}") # Requires both
Click 'True'/'False' above to see how `and` works!
🤷 The `or` Operator: At Least One Must Be True
Use `or` when you need only ONE of the conditions to be `True` for the overall result to be `True`.
Imagine getting a discount if you're a senior citizen OR a club member. You don't need to be both!
In Python, `condition1 or condition2` results in `True` if `condition1` is `True`, or if `condition2` is `True`, or if both are `True`. It's only `False` if BOTH are `False`.
It's like having two different keys that can open the same door.
is_weekend = True
is_holiday = False
day_off = is_weekend
or is_holiday
print(f"Is it a day off? {day_off
}") # Only one needs to be True
Click 'True'/'False' above to see how `or` works!
🔄 The `not` Operator: Flip the Truth
Use `not` to reverse the boolean value of a condition. `True` becomes `False`, and `False` becomes `True`.
This is useful when you want to check if something is NOT the case. For example, proceed only if the user is `not` an admin.
In Python, `not condition` evaluates to the opposite boolean value of `condition`.
Think of it as a switch that flips the light: if it was ON (`True`), `not` makes it OFF (`False`), and vice-versa.
is_logged_in = False
show_login_prompt = not is_logged_in
print(f"Show login prompt? {show_login_prompt
}") # Flips the value
Click 'True'/'False' above to see how `not` flips the result!
🧩 Combining Operators: Complex Logic
You can chain `and`, `or`, and `not` together using parentheses `()` to control the order, just like in math. This is where Decomposition Decomposition helps break down complex rules.
Consider granting access to a special feature if a user is (an admin OR a VIP) AND is NOT banned.
We decompose this:
- Check if `is_admin` OR `is_vip`.
- Check if `is_banned` is NOT True.
- Require BOTH results (from step 1 AND step 2) to be True.
Parentheses `()` ensure the `or` is evaluated first before the `and`.
is_admin = False
is_vip = True
is_banned = False
# Evaluate (is_admin or is_vip) first -> True
# Evaluate (not is_banned) second -> True
# Evaluate (True and True) last -> True
has_special_access = (is_admin
or is_vip
) and not is_banned
print(f"Special access granted? {has_special_access
}")
🧠 Quick Check!
Module 9 Theory Complete!
Excellent! You've learned how to combine conditions using `and`, `or`, and `not`. This is crucial for creating programs that make logical decisions.
Ready to practice building these conditions? Head to the Practice Zone or the Advanced Practice.