Back to Network AutomationPython fundamentals

Python Logical Operators

Combine multiple conditions with and and or, reverse a condition with not, and interpret the resulting boolean value.

BeginnerLogicBooleansPython

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
  2. AND
  3. OR
  4. NOT
  5. Network Automation Example
  6. Guided Practice
  7. Conclusion

1. Logical Operators Infographic

Use this visual reference to compare the syntax, truth tables, and example results for all three logical operators.

Python logical operators infographic with syntax, truth tables, examples, key takeaways, and network automation uses for and, or, and not
Python uses 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.

XYX and Y
FalseFalseFalse
FalseTrueFalse
TrueFalseFalse
TrueTrueTrue

Both conditions pass

>>> 10 > 5 and 6 > 4
True

One condition fails

>>> 10 > 5 and 10 > 11
False

Logical 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
True

3. OR

The or operator returns True when at least one condition is true. It returns False only when both conditions are false.

XYX or Y
FalseFalseFalse
FalseTrueTrue
TrueFalseTrue
TrueTrueTrue

Both conditions pass

>>> 1 == 1 or 2 == 2
True

One condition passes

>>> 1 == 1 or 2 > 3
True

When neither comparison passes, the complete expression is false:

Both conditions fail

>>> 1 == 2 or 2 == 3
False

4. NOT

The not operator reverses the truth value of one condition. A true condition becomes False, and a false condition becomes True.

Xnot X
FalseTrue
TrueFalse

Reverse a true result

>>> not (1 == 1)
False

Reverse a false result

>>> not (5 > 10)
True
Readability tip: Parentheses are optional in many simple expressions, but they can make the condition being reversed easier to see.

5. 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.

Keep conditions explicit: Long expressions are harder to review. Store meaningful intermediate results, such as version_approved, before combining them.

6. Guided Practice

Task 1: Predict the boolean result

  1. Predict 8 > 3 and 4 == 4.
  2. Predict "up" == "down" or 22 < 30.
  3. Predict not (5 == 5).
  4. Run each expression in the interpreter.

Expected result: The expressions return True, True, and False.

Task 2: Check a saved interface state

  1. Set admin_up and line_up to True.
  2. Create interface_ready = admin_up and line_up.
  3. Print the result.
  4. Change line_up to False and 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: and returns True only when both conditions are true.
  • OR: or returns True when either condition is true.
  • NOT: not reverses 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.