What are Python Boolean Values and Operators

What are Python Boolean Values and Operators
python-boolean-values-operators

Learning Python Boolean values and operators is part of the series — Zero to Network Automation. In this post, we’ll explore Python Boolean values and operators — the building blocks of decision-making in scripts and network automation.

In network automation, we constantly make decisions — “Is the interface up?”, “Did the script succeed?”, “Is the value correct?”.

In Python, these kinds of decisions rely on Boolean values — True or False.

Understanding how Boolean operators like and, or, and not work will help you write logic that responds intelligently to network states.

Boolean Values

Boolean operators are used to compare two values. The end result is a single Boolean value that will be either True or False.

Booleans are used in Python flow control. They determine whether a block of code will run or not.

True will always have an uppercase T.

False will always have an uppercase F.

Neither will be a Boolean value if you use the lowercase letters:

>>> true
Traceback (most recent call last):
  File "<python-input-52>", line 1, in <module>
    true
NameError: name 'true' is not defined. Did you mean: 'True'?

>>> false
Traceback (most recent call last):
  File "<python-input-53>", line 1, in <module>
    false
NameError: name 'false' is not defined. Did you mean: 'False'?

Boolean Operators

There are three types of Boolean operators:

  • and
  • or
  • not

We use these Boolean operators to compare Boolean values within an expression.

OperatorDescriptionExampleResult
andTrue if both are TrueTrue and TrueTrue
orTrue if either is TrueTrue or FalseTrue
notReverse Boolean valuenot TrueFalse

The and operator takes two Boolean values such as:

>>> 10 == 10 and 50 == 50
True

Python looks at the first expression, 10 == 10, which they are the same so therefore it is True.

Then evaluates 50 == 50, which they are the same so therefore it is True. And since it is True and True, the expression results in True.

The or operator will look at the expression and if either of them is True then the result is True unless both values are False.

>>> 10 == 10 or 20 == 21
True

>>> 10 == 11 or 20 == 21
False

The third Boolean operator, not, does not look at two values. The not operator evaluates to the opposite of the Boolean value.

For example:

>>> not True
False

A little odd, right? The way I read it is “Not True” so therefore it is False.

>>> not 10 == 10
False

>>> not 10 == 11
True

Booleans are the foundation of how Python makes decisions.

As we move deeper into network automation, you’ll see how they help us validate device states, control scripts, and handle conditions dynamically.

Next up, we’ll start applying this logic to flow control using if, elif, and else.