🧠 Logical Operators
Last Updated: 16th August 2025
Logical operators are used to combine conditional statements (expressions that return True/False).
They allow us to make complex decisions in code.
Hinglish Tip 🗣: Logical operators ko use karke aap multiple conditions ek saath check kar sakte ho.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| and | Returns True if both conditions are True | (5 > 2) and (10 > 5) | True |
| or | Returns True if at least one condition is True | (5 < 2) or (10 > 5) | True |
| not | Reverses the result (True → False, False → True) | not(5 == 5) | False |
✏ Syntax & Examples
a = 10
b = 20
print(a > 5 and b > 15) # True (both are True)
print(a > 5 or b < 10) # True (one condition is True)
print(not(a == 10)) # False (a == 10 is True, so not → False)
⚠️ Quick Notes
- and → True only if both sides are True.
- or → True if any one side is True.
- not → flips the result (True ↔ False).
- Python uses short-circuit evaluation:
- and stops if first condition is False.
- or stops if first condition is True.
Hinglish Tip 🗣: Short-circuit matlab Python unnecessary conditions check nahi karega agar pehle hi answer mil gaya.
- and with Truthy/Falsy Values:
Expression Result Explanation True and True True Both True → returns last (True) 1 and 2 2 Both truthy → returns last (2) 2 and 3 and 4 4 All truthy → returns last 0 and 5 0 0 is falsy → returned immediately '' and 'Python' '' Empty string is falsy 5 and 0 and 1 0 Stops at 0 (first falsy)
💡 Quick Practice
- Write a condition: Check if a number n is between 10 and 20 using and.
- Use or to check if a number is negative or zero.
- Use not to invert the result of (5 > 10).
- Try (True and False) or True. Predict the result.
- Exercises