Python Logical Operators
Combine multiple conditions with and and or, reverse a condition with not, and interpret the resulting boolean value.
Python Logical Operators at a Glance
Logical operators are one of the main Python operator types. They connect conditions that usually come from comparison operators and produce a boolean result.
and
Returns True only when both conditions are true.
or
Returns True when at least one condition is true.
not
Reverses a condition's truth value.
In This Lesson
1. Logical Operators Infographic
Use this visual reference to compare the syntax, truth tables, and example results for all three logical operators.
and, or, and not to combine or reverse conditions.2. AND
The and operator returns True only when the condition on each side is true. If either condition is false, the complete expression is false.
| X | Y | X and Y |
|---|---|---|
| False | False | False |
| False | True | False |
| True | False | False |
| True | True | True |
Both conditions pass
>>> 10 > 5 and 6 > 4
TrueOne condition fails
>>> 10 > 5 and 10 > 11
FalseLogical operators are not limited to numeric comparisons. The next expression is true because the string matches and the numeric comparison also passes:
Combine different checks
>>> "switch" == "switch" and 10 > 5
True3. OR
The or operator returns True when at least one condition is true. It returns False only when both conditions are false.
| X | Y | X or Y |
|---|---|---|
| False | False | False |
| False | True | True |
| True | False | True |
| True | True | True |
Both conditions pass
>>> 1 == 1 or 2 == 2
TrueOne condition passes
>>> 1 == 1 or 2 > 3
TrueWhen neither comparison passes, the complete expression is false:
Both conditions fail
>>> 1 == 2 or 2 == 3
False4. NOT
The not operator reverses the truth value of one condition. A true condition becomes False, and a false condition becomes True.
| X | not X |
|---|---|
| False | True |
| True | False |
Reverse a true result
>>> not (1 == 1)
FalseReverse a false result
>>> not (5 > 10)
True5. Network Automation Example
Logical operators help a script combine several facts before it reports a device as ready. This read-only example checks reachability, software version, and maintenance state:
Evaluate saved device facts
reachable = True
software_version = "17.9.4"
approved_versions = ("17.9.4", "17.12.3")
in_maintenance = False
version_approved = software_version == "17.9.4" or software_version == "17.12.3"
ready_for_check = reachable and version_approved and not in_maintenance
print(ready_for_check)The output is True. The device is reachable, its version is one of the approved choices, and not in_maintenance confirms that the maintenance flag is false.
version_approved, before combining them.6. Guided Practice
Task 1: Predict the boolean result
- Predict
8 > 3 and 4 == 4. - Predict
"up" == "down" or 22 < 30. - Predict
not (5 == 5). - Run each expression in the interpreter.
Expected result: The expressions return True, True, and False.
Task 2: Check a saved interface state
- Set
admin_upandline_uptoTrue. - Create
interface_ready = admin_up and line_up. - Print the result.
- Change
line_uptoFalseand run it again.
Expected result: The first run prints True; the second prints False.
7. Conclusion
You learned how to combine and reverse boolean conditions with Python logical operators:
- AND:
andreturnsTrueonly when both conditions are true. - OR:
orreturnsTruewhen either condition is true. - NOT:
notreverses the truth value of a condition.
These operators are commonly combined with comparisons in if, elif, and else logic. They let an automation script make one clear decision from several pieces of operational state.
Python Logical Operators Frequently Asked Questions
What do Python logical operators return?
When logical operators combine boolean conditions, the result is True or False.
What is the difference between and and or in Python?
The and operator requires both conditions to be true. The or operator requires at least one condition to be true.
What does the not operator do in Python?
The not operator reverses a condition's truth value: True becomes False, and False becomes True.