Logical Operators
Logical Operators in Python are used to combine multiple conditions and return a result as True or False.
Real-Life Example
Logical operators are like decisions in real life:
- If you have a ticket AND ID → you can enter
- If you have money OR card → you can pay
- If it is NOT raining → you can go outside
Explanation
These operators help in decision-making by checking more than one condition at the same time.
They are commonly used in:
ifstatements- Loops
- Real-life problem solving
They always return a Boolean value (True/False).
Types of Logical Operators
- and → Returns True if both conditions are True
- or → Returns True if at least one condition is True
- not → Reverses the result (True → False, False → True)
Example Program 1
a = 10
print(a > 5 and a < 20)
print(a > 15 or a < 20)
print(not(a > 5))
Output:
True
True
False
Example Program 2
age = 18
has_id = True
if age >= 18 and has_id:
print("Eligible to vote")
else:
print("Not eligible")
Output:
Eligible to vote
Explanation
age >= 18→ checks if the person is 18 or olderhas_id→ checks if the person has an ID proofand→ both conditions must be True
Common Beginner Mistakes
- Confusing and / or
- Forgetting brackets
- Misunderstanding True/False
- Wrong condition combinations
Key Points
- Logical operators combine conditions
- They return True or False
- Used in decision-making programs
- Important for building logic in Python
Tips
- Use and when both conditions must be true
- Use or when at least one condition is enough
- Use not to reverse results
- Practice using if conditions