Chapter 11: Logical Operators¶
🚀 Open Notebook¶
📺 Video Tutorial¶
Logical operators in Python are easy 🔣 (7:56)
📚 What You’ll Learn¶
Combine multiple conditions using logical operators to create powerful decision-making logic!
🎯 Learning Objectives¶
Understand AND, OR, and NOT logical operators
Combine multiple conditions in if statements
Create complex decision-making logic
Use truth tables to understand operator behavior
📖 Concept Explanation¶
Logical Operators¶
Logical operators combine boolean expressions (conditions) to create more complex logic.
Operator |
Description |
Example |
|---|---|---|
|
Both conditions must be True |
|
|
At least one condition must be True |
|
|
Reverses the boolean value |
|
AND Operator¶
Returns True only if both conditions are True:
if temp > 30 and is_sunny:
print("Hot and sunny!")
Truth Table for AND:
Condition 1 |
Condition 2 |
Result |
|---|---|---|
True |
True |
True |
True |
False |
False |
False |
True |
False |
False |
False |
False |
OR Operator¶
Returns True if at least one condition is True:
if is_weekend or is_holiday:
print("Day off!")
Truth Table for OR:
Condition 1 |
Condition 2 |
Result |
|---|---|---|
True |
True |
True |
True |
False |
True |
False |
True |
True |
False |
False |
False |
NOT Operator¶
Reverses the boolean value:
if not is_raining:
print("No umbrella needed!")
Truth Table for NOT:
Condition |
Result |
|---|---|
True |
False |
False |
True |
💡 Examples¶
Example 1: Age and License Check¶
age = 20
has_license = True
if age >= 18 and has_license:
print("You can drive!")
else:
print("You cannot drive yet.")
Example 2: Weekend or Holiday¶
is_weekend = False
is_holiday = True
if is_weekend or is_holiday:
print("No work today!")
else:
print("Time to work.")
Example 3: Weather Advisory¶
temp = 35
is_sunny = True
is_raining = False
if temp > 30 and is_sunny and not is_raining:
print("Perfect beach weather!")
Example 4: Login System¶
username = "admin"
password = "secret123"
is_verified = True
if (username == "admin" and password == "secret123") and is_verified:
print("Access granted!")
else:
print("Access denied!")
✍️ Practice Exercises¶
Voting Eligibility: Check if person can vote (age >= 18 AND is_citizen)
Discount Calculator: Apply discount if purchase > $100 OR is_member
Alarm System: Sound alarm if is_night AND (motion_detected OR door_open)
Grade Pass/Fail: Pass if score >= 60 AND attendance >= 75%
Weather Outfit: Suggest outfit based on temp AND (is_raining OR is_snowing)
Access Control: Grant access if (is_admin OR is_manager) AND is_active
🔍 Common Mistakes¶
Mistake 1: Confusing AND with OR¶
# Wrong: This is too restrictive
if day == "Saturday" and day == "Sunday": # day can't be both!
print("Weekend")
# Correct:
if day == "Saturday" or day == "Sunday":
print("Weekend")
Mistake 2: Forgetting Parentheses¶
# Ambiguous:
if age > 18 and is_student or has_id # Unclear precedence
# Clear:
if (age > 18 and is_student) or has_id # Much better!
Mistake 3: Using = Instead of ==¶
# Wrong:
if username = "admin": # Assignment, not comparison!
# Correct:
if username == "admin": # Comparison
📝 Operator Precedence¶
Python evaluates operators in this order:
Parentheses
()notandor
# Without parentheses
result = True or False and False # True (and evaluated first)
# With parentheses (recommended for clarity)
result = True or (False and False) # True
result = (True or False) and False # False
🎮 Real-World Examples¶
Example: E-commerce Discount¶
total = 150
is_member = True
has_coupon = False
# Member discount OR coupon discount
if (total >= 100 and is_member) or has_coupon:
discount = total * 0.20
final_price = total - discount
print(f"Discount applied! Final price: ${final_price}")
else:
print(f"Total: ${total}")
Example: Security System¶
is_armed = True
is_door_open = True
is_night = True
has_motion = False
if is_armed and (is_door_open or (is_night and has_motion)):
print("🚨 ALERT! Intrusion detected!")
else:
print("✓ System normal")
🚀 Challenge Projects¶
Smart Thermostat: Turn on AC if (temp > 75 and is_home) or override_on
Library Fine Calculator: Fine if overdue AND (not is_student OR days > 7)
Flight Booking: Available if (seats > 0) and (has_passport or is_domestic)
Game Level Unlock: Unlock if (score >= 1000 and level >= 5) or has_premium
🎓 Key Takeaways from Video¶
Conditionals execute code based on conditions
Use if-elif-else for conditional logic
Follow along with the video for hands-on practice
💡 These points cover the main concepts from the video tutorial to help reinforce your learning.
🔗 Next Chapter¶
Continue to Chapter 12: Conditional Expressions to learn shorthand if-else statements!