What are Logical Operators ?
Logical operators are symbols or keywords used in Python to combine multiple conditions or reverse a condition.
While relational operators compare individual values, logical operators evaluate multiple comparisons together to make more complex decisions. Just like relational operators, logical operators always result in a Boolean value: either True or False. They are foundational for setting up multi-layered rules inside if-else blocks and control loops.
Python Logical Operators Table
Assuming we are evaluating two conditions: Condition_A and Condition_B
| Operator | Name | Syntax |
|---|---|---|
| and | Logical AND | A and B |
| or | Logical OR | A or B |
| not | Logical NOT | not A |
1. Logical AND Operator (and)
The and operator links two conditions together. It acts like a strict gatekeeper: the final result is True only if every single condition evaluates to True. If even one condition is False, the entire expression becomes False.
# Bank ATM withdrawal verification parameters
has_correct_pin = True
has_sufficient_balance = False
# A user can withdraw money only if BOTH conditions pass
can_withdraw = has_correct_pin and has_sufficient_balance
print("Allow cash withdrawal:", can_withdraw)
# Output: Allow cash withdrawal: False
2. Logical OR Operator (or)
The or operator is highly flexible. It acts like an open option: the final result evaluates to True if at least one of the conditions is True. It only returns False if absolutely all connected conditions fail.
# Streaming platform discount check
is_student = False
has_promo_coupon = True
# User gets a discount if they match AT LEAST ONE criteria
gets_discount = is_student or has_promo_coupon
print("Apply special discount rate:", gets_discount)
# Output: Apply special discount rate: True
3. Logical NOT Operator (not)
The not operator is a unary operator, meaning it works on only a single condition or variable. It acts like a mirror or an invert toggle: it completely reverses the Boolean state. If an expression is True, not makes it False. If it is False, not flips it to True.
# App background processing check
is_loading_finished = True
# Checking if the application is still busy processing data
is_app_busy = not is_loading_finished
print("Display loading spinner icon:", is_app_busy)
# Output: Display loading spinner icon: False
Advanced Python Concept: Short-Circuit Evaluation
Python is highly optimized. When running logical
operations, it uses a smart shortcut feature called
Short-Circuit Evaluation. This means Python will
stop evaluating a long condition as soon as the
final answer is certain.
For and operations :If the first
condition evaluates to False, Python already knows
the total expression must be False. It completely
skips looking at the second half of your code to
save processing time.
For or operations : If the first
condition evaluates to True, Python already knows
the final result must be True. It immediately stops
checking any further conditions.
# A safe division condition using short-circuiting
divisor = 0
number = 10
# Python checks 'divisor != 0' first. It sees 'False' (since divisor is 0).
# Because it uses 'and', it stops immediately and avoids checking 'number / divisor'.
# This saves your program from throwing a ZeroDivisionError crash!
safe_check = (divisor != 0) and (number / divisor > 2)
print("Calculation safe to process:", safe_check)
# Output: Calculation safe to process: False