-
Notifications
You must be signed in to change notification settings - Fork 0
Logical Operators
Logical operators help us combine multiple conditions in an if statement.
Python has three logical operators:
| Operator | Symbol | Example | Description |
|---|---|---|---|
| AND | and |
x > 5 and x < 10 |
✅ True if both conditions are True |
| OR | or |
x > 5 or x < 3 |
✅ True if at least one condition is True |
| NOT | not |
not(x > 5) |
✅ Reverses the condition (True → False, False → True) |
The and operator checks two conditions. If both are True, the result is True. Otherwise, it’s False.
🔹 Example: Checking Age & Ticket 🎟️
age = 20
has_ticket = True
if age >= 18 and has_ticket:
print("You can enter the movie! 🎬")
else:
print("Sorry, you cannot enter.")💡 Output:
You can enter the movie! 🎬
✅ age >= 18 → True
✅ has_ticket → True
✅ True and True → True (So, the message is printed!)
❌ If age = 16 and has_ticket = True, the result would be:
Sorry, you cannot enter.
🚀 Both conditions must be True for AND to work!
The or operator checks two conditions. If at least one is True, the result is True.
🔹 Example: Checking Free Pass or Age
age = 16
has_free_pass = True
if age >= 18 or has_free_pass:
print("You can enter the concert! 🎵")
else:
print("Sorry, you cannot enter.")💡 Output:
You can enter the concert! 🎵
✅ age >= 18 → False
✅ has_free_pass → True
✅ False or True → True (So, the message is printed!)
❌ If age = 16 and has_free_pass = False, the result would be:
Sorry, you cannot enter.
🚀 Only one condition needs to be True for OR to work!
The not operator reverses a condition.
🔹 Example: Checking if it's NOT Raining ☀️
is_raining = False
if not is_raining:
print("Go outside and play! ☀️")
else:
print("Stay inside. ☔")💡 Output:
Go outside and play! ☀️
✅ is_raining = False
✅ not(False) → True
✅ The if condition becomes True, so we print the message.
❌ If is_raining = True, the result would be:
Stay inside. ☔
🚀 NOT reverses True → False and False → True!
We can combine and, or, and not for complex conditions!
🔹 Example: Checking for Discount 💰
age = 65
is_student = False
if age >= 60 or is_student:
print("You get a discount! 🎉")
else:
print("No discount available.")💡 Output:
You get a discount! 🎉
✅ age >= 60 → True
✅ is_student → False
✅ True or False → True (So, the discount is applied!)
🔹 Example: Checking Exam Eligibility 🎓
has_passed_exam = True
has_attended_classes = False
if has_passed_exam and not has_attended_classes:
print("You passed, but you missed classes! 📚")💡 Output:
You passed, but you missed classes! 📚
✅ has_passed_exam = True
✅ has_attended_classes = False
✅ not False → True
✅ True and True → True (Message is printed!)
Here’s a quick truth table to summarize how and, or, and not work!
| A | B | A and B |
|---|---|---|
| True | True | True |
| True | False | False |
| False | True | False |
| False | False | False |
| A | B | A or B |
|---|---|---|
| True | True | True |
| True | False | True |
| False | True | True |
| False | False | False |
| A | not A |
|---|---|
| True | False |
| False | True |
❌ Using = instead of ==
if x = 10 and y = 20: # ❌ SyntaxError✅ Fix: Use == for comparisons
if x == 10 and y == 20: # ✅ Correct❌ Forgetting Parentheses with NOT (not)
if not x > 10 and y < 5: # ❌ May not work as expected!✅ Fix: Use parentheses for clarity
if not (x > 10 and y < 5): # ✅ Correct❌ Overcomplicating Conditions
if (x > 5 and x < 10) or (x >= 10 and x < 20): # ❌ Too complex✅ Fix: Use a simple range check
if 5 < x < 20: # ✅ Cleaner and better✔️ AND (and) → Both conditions must be True.
✔️ OR (or) → At least one condition must be True.
✔️ NOT (not) → Reverses True and False.
✔️ Logical operators can be combined for powerful conditions.
✔️ Parentheses help organize complex conditions.
has_high_income = True
has_good_credit = True
has_criminal_record = False
if has_high_income and has_good_credit and not has_criminal_record:
print("Eligible for loan")