# Python Boolean Operators

Python Boolean Operators are used to perform useful operations in Python programming language. Here is how you can learn to perform them.

## Python Boolean Operators: `and` and `or` are not guaranteed to return a boolean

When you use or, it will either return the first value in the expression if it’s true, else it will blindly return the second value. I.e. or is equivalent to:

`def or_(a, b):if a:return aelse:return b`

For and, it will return its first value if it’s false, else it returns the last value:

`def and_(a, b):if not a:return aelse:return b`

## Python Boolean Operators: A simple example

In Python you can compare a single element using two binary operators–one on either side:

`if 3.14 < x < 3.142:print("x is near pi")`
`In many (most?) programming languages, this would be evaluated in a way contrary to regular math: (3.14 < x) < 3.142, but in Python it is treated like 3.14 < x and x < 3.142, just like most non-programmers would expect.`

## Python Boolean Operators: Short-circuit evaluation

Python minimally evaluates Boolean expressions.

`def true_func():… print("true_func()")… return True…def false_func():… print("false_func()")… return False…true_func() or false_func() true_func()Truefalse_func() or true_func() false_func()true_func() Truetrue_func() and false_func() true_func()false_func() Falsefalse_func() and false_func()`
`false_func()False`

## Python Boolean Operators: and

Evaluates to the second argument if and only if both of the arguments are truthy. Otherwise evaluates to the first falsey argument.

`x = Truey = Truez = x and y # z = Truex = Truey = Falsez = x and y # z = Falsex = Falsey = Truez = x and y # z = Falsex = Falsey = Falsez = x and y # z = Falsex = 1y = 1z = x and y # z = y, so z = 1, see `and` and `or` are not guaranteed to be a booleanx = 0y = 1z = x and y # z = x, so z = 0 (see above)x = 1y = 0z = x and y # z = y, so z = 0 (see above)x = 0y = 0z = x and y # z = x, so z = 0 (see above)`

The 1’s in the above example can be changed to any truthy value, and the 0’s can be changed to any falsey value.

## Boolean Operator: or

Evaluates to the first truthy argument if either one of the arguments is truthy. If both arguments are falsey, evaluates to the second argument.

`x = Truey = Truez = x or y # z = Truex = Truey = Falsez = x or y # z = Truex = Falsey = Truez = x or y # z = True`
`x = Falsey = Falsez = x or y # z = Falsex = 1y = 1z = x or y # z = x, so z = 1, see `and` and `or` are not guaranteed to be a booleanx = 1y = 0z = x or y # z = x, so z = 1 (see above)x = 0y = 1z = x or y # z = y, so z = 1 (see above)x = 0y = 0z = x or y # z = y, so z = 0 (see above)`

The 1’s in the above example can be changed to any truthy value, and the 0’s can be changed to any falsey value.

## Lastly: not

It returns the opposite of the following statement:

`x = Truey = not x # y = Falsex = Falsey = not x # y = True`

Previous articlePython Bitwise Operators
Next articlePython Operator Precedence
SHARE