Logical and Boolean Operators

George Boole was an Irish scientist who worked out many of the principles of binary logic long before the invention of computers. We refer to this branch of mathematics as Boolean algebra. In binary, there are only two values: zero and one. They can also be represented by the symbols True and False. It is useful to be able to check more than one comparison operator. For this we can use Boolean tests. I tried these out at the Python command prompt, results are in the right-most column.

Operator
Description
Example

and

Return True if both statements are True.

>>> 4>3 and 1==1 True

or

Return True if either statement is True

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

not

Inverts the result

>>> not(4>3 or 1==1) False

in

Returns True if a value is in a sequence

>>> ‘J’ in “JOR” True

Checking a Boolean value for a variable which is not a Boolean is also possible.

A variable which is 0 or None or an empty string “” will evaluate as False.

A variable which is and value other than 0 or a string such as “Hello” will evaluate as True.