π§ Python Logical Operators Explained β AND, OR, NOT with Examples
Logical operators in Python are used to combine conditional statements. They are essential in decision making, controlling the flow of programs using if, while, and other control structures.
π What Are Logical Operators?
Python provides three main logical operators:
| Operator | Description | Syntax Example | Result Condition |
|---|---|---|---|
and | Logical AND | x > 5 and x < 10 | β True only if both are True |
or | Logical OR | x > 5 or x < 3 | β True if at least one is True |
not | Logical NOT (Negation) | not (x > 10) | β True if expression is False |
π Truth Tables
πΉ and Operator
A | B | A and B |
|---|---|---|
| True | True | True |
| True | False | False |
| False | True | False |
| False | False | False |
β
Only returns True if both conditions are True.
πΉ or Operator
A | B | A or B |
|---|---|---|
| True | True | True |
| True | False | True |
| False | True | True |
| False | False | False |
β
Returns True if any one condition is True.
πΉ not Operator
A | not A |
|---|---|
| True | False |
| False | True |
β Negates the condition (inverse of truth value).
π§ͺ Examples
β
and Usage:
x = 8
y = 21
if (x < 10) and (y > 20):
print("Condition met")
π Explanation:
x < 10β Truey > 20β True- Both are True β Executes the print statement.
β
or Usage:
age = 70
if age < 12 or age > 65:
print("Eligible for discount")
π One condition age > 65 is True β Statement is executed.
β
not Usage:
is_logged_in = False
if not is_logged_in:
print("Please log in")
π not False β True β Displays message.
βοΈ Short-Circuit Behavior
Python evaluates expressions from left to right:
A or Bβ if A is True, B is not evaluated.A and Bβ if A is False, B is not evaluated.
π‘ Useful for optimizing performance and preventing unnecessary function calls:
def check():
print("Check called")
return True
if False and check():
print("This wonβt print")
Output: Nothing is printed because False and stops evaluation.
β οΈ Operator Precedence
From highest to lowest:
notandor
π Example:
True or False and False
β
Evaluates as: True or (False and False) β True
π‘ Pythonic Tips
- Prefer
if x:overif x != 0:when checking for truthiness. - Use logical chaining like
10 < x < 20instead ofx > 10 and x < 20(Python supports this!). - Use
notto simplify negative logic:if not x == yβif x != y.
π Summary
| Operator | Description | Truth Behavior |
|---|---|---|
and | True if both conditions are true | Stops at first False |
or | True if any condition is true | Stops at first True |
not | Inverts truth value | Highest precedence |
Logical operators are powerful tools for building decision-making logic in Python. Mastering them is key to writing clear and efficient code.
βFAQs β Logical Operators in Python
β What does and return in Python?
It returns the first falsy value or the last value if all are truthy. Ex: 3 and 5 β 5.
β How is or used for setting defaults?
You can use or to set fallback values:
name = input("Name: ") or "Guest"
β Does not work on numbers?
Yes. not 0 β True, not 5 β False. Python treats 0 as False and any non-zero number as True.
β Can I use multiple and/or together?
Yes, use parentheses for clarity:
if (x > 5 and y < 10) or z == 0:
Share Now :
