Logical Operators in Python
Logical operators are used to combine multiple conditions (expressions) and produce a boolean result: True
or False
.
Logical operators play a key role in decision-making and are commonly used in if
statements and loops.
List of Logical Operators
Operator | Keyword | Description |
---|---|---|
and | and | True if both conditions are True |
or | or | True if at least one condition is True |
not | not | Reverses the result: True becomes False, False becomes True |
1. and
Operator
Returns True
only if both conditions are True
.
logical_and.py
a = 5
b = 10
print(a < 10 and b > 5)
output.txt
True
Example 2: One Condition False
logical_and_false.py
x = 3
y = 7
print(x > 5 and y < 10)
output.txt
False
Example 3: Combining String and Number
logical_and_mixed.py
name = "Alice"
age = 20
print(name == "Alice" and age >= 18)
output.txt
True
Explanation:
- If either side of the
and
operator isFalse
, the whole expression evaluates toFalse
.
2. or
Operator
Returns True
if at least one condition is True
.
logical_or.py
a = 5
b = 10
print(a > 10 or b > 5)
output.txt
True
Example 2: Both Conditions False
logical_or_false.py
x = 1
y = 2
print(x > 5 or y > 10)
output.txt
False
Example 3: Multiple Conditions
logical_or_strings.py
status = "inactive"
score = 80
print(status == "active" or score >= 75)
output.txt
True
Explanation:
- If any one condition is
True
, the entire expression evaluates toTrue
.
3. not
Operator
Reverses the boolean result: True
becomes False
, and vice versa.
logical_not.py
is_logged_in = True
print(not is_logged_in)
output.txt
False
Example 2: Used with and
logical_not_and.py
x = 10
y = 5
print(not (x < 20 and y > 2))
output.txt
False
Example 3: Used with or
logical_not_or.py
is_admin = False
is_user = True
print(not (is_admin or is_user))
output.txt
False
Explanation:
- The
not
operator inverts the result of an expression. - Very useful for conditions where you want to check for the opposite.
Combining Logical Operators
You can combine and
, or
, and not
in a single expression:
logical_combined.py
age = 25
is_student = False
print((age >= 18 and age <= 30) or is_student)
output.txt
True
Summary
and
returnsTrue
only if both conditions areTrue
.or
returnsTrue
if any one condition isTrue
.not
inverts the result of an expression.- Logical operators are widely used in conditional logic to build complex decision-making rules.