Python Operators. Managing variables and their values Part 2

In the last post we talked about operators in Python and we introduced some of the most common ones.

Some of the operations we talked about was Booleans, like the Not-Equal, Equal, Greater-Than among others.

We are going to see some more Boolean operations in this post.

Let’s start with the not operator.

The not operator

Python uses this operator to perform Boolean negation. If an expression is False, its negation is True. If an expression is True it is False.

Being unary, it uses only one operand.

This operand could be either a Boolean expression or any Python object.

Even user-defined objects work.

Here the not operator performs Boolean negation to the zero and one variables.
(click on the image to open in a new tab)

The and operator

Another boolean operator in Python is the and operator.

In Python, this operator is a logical operator that checks two conditions and outputs True when both are met.

By using it, we can create more complicated logical expressions.

The and operator checks the first value and then checks the second one provided that the first check was True.

If the first operator evaluates to False, it stops right there.

So and needs both values to be True. That’s what we are checking for.

If either one is False, the result of the operation will be False.

Different scenarios with the and operator.
(click on the image to open in a new tab)

The or operator

Just the opposite is the Boolean operator or.

This operator is only going to look at the second only if the first is False.

How the or operator works.
(click on the image to open in a new tab)

In the first example x is True so no need for that.

In the second one the first (y) is False so it checks the second (y)which is also False hence the result is False.

In the next REPL, first value (x) is True so the evaluation stops right there.

In the last one, the first value (y) is False so second value will be evaluated. Since it is True, the result is also True.

Although my blog doesn’t support comments, feel free to reply via email or X.