24. Boolean Operators#
My first programming language was C language, Where I got to meet our boolean operator friends which are &&
for and
, ||
for or
and !
for not
. I was like why symbols, can we just use and
, or
and not
?
Then I got myself introduced to the Python
. The readability of code in Python is top-notch compared to other languages, which holds good even for boolean operators and
, or
, and not
😎.
Yup! In Python, boolean operators and
, or
and not
are themselves instead of symbols.
Let’s get to know more about Boolean operators in Python!
💡 Before diving into Boolean operators, it’s a little necessary to drift into Truth Value Testing in Python.
24.1. Truth Value Testing#
Every object in Python can be tested for truth value.
By default, an object is considered true unless its class defines either a __bool__()
method that returns False or a __len__()
method that returns zero, when called with the object. No worries, we would learn about __bool__
and __len__
Once we get into the chapters about classes.
Here are the built-ins defined False
for Truth value testing.
constants defined to be false: None and False.
zero of any numeric type: 0, 0.0, 0j, Decimal(0), Fraction(0, 1)
empty sequences and collections: “”, (), [], {}, set(), range(0)
To find the Truth Value of objects, we can do as bool(<obj>)
. All the above mentioned objects returns False
when tried bool(<obj>)
. Let’s see examples:
print(f"{bool(None)}") # On None.
print(f"{bool(False)}") # On False.
print(f"{bool(0)}") # On Integer 0.
print(f"{bool(0.0)}") # On Float 0.0
print(f"{bool(0j)}") # On Complex 0j.
empty_string = ""
print(f"{bool(empty_string)}") # On empty string.
print(f"{bool(())}") # On empty tuple.
print(f"{bool([])}") # On empty list.
print(f"{bool({})}") # On empty dictionary.
print(f"{bool(set())}") # On empty set.
print(f"{bool(range(0))}") # On range(0).
False
False
False
False
False
False
False
False
False
False
False
Hurray! We see the Truth values of all the above mentioned objects are returning False
❌ as expected.
In Python, we have 3 boolean operators namely and
, or
and not
. let’s see about the result obtained based on the operands:
Boolean operators always result object based on the Truth values of the Operands.
24.2. Boolean Operation and results#
Operation |
Result |
---|---|
x |
if x’s truth value is false, then y, else x |
x |
if x’s truth value is false, then x, else y |
|
if x is false, then True, else False |
let’s try the above table 😊
24.3. or
operation on objects.#
In x or y
, if x’s truth value is false, then y, else x, which means or
evaluates to first operand if either one of the operands is truthy, Otherwise it evaluates only to the second operand.
# Boolean objects
print(True or True)
print(True or False)
print(False or True)
print(False or False)
True
True
True
False
# Numerals
print(1 or 0)
print(0 or 1)
1
1
Let’s know about how we got the above result.
1 or 0
resulted in 1 and 0 or 1
resulted 1 again. Let’s remember what we have said about and in the table: if x’s truth value is false, then y, else x. Yup, it works.
In 1 or 0
, the truth value of 1 is True, so according to the above statement, it results in the first operand which is 1.
In 0 or 1
, the truth value of 0 is False, so according to the above statement, it results in the second operand itself which is 1.
24.4. and
operation on objects.#
In x and y
, if x’s truth value is false, then x, else y, which means and
evaluates to second operand if and only if both the operands are truthy, Otherwise it evaluates only to the first operand.
# Boolean objects
print(True and True)
print(True and False)
print(False and True)
print(False and False)
True
False
False
False
# Numerals
print(1 and 0)
print(0 and 1)
0
0
Let’s know about how we got the above result.
1 and 0
resulted in 0 and 0 and 1
resulted 0 again. Let’s remember what we have said about and
in the table: if x’s truth value is false, then x, else y. Yup, it works.
In 1 and 0
, the truth value of 1
is True
, so according to the above statement, it results in the second operand which is 0
.
In 0 and 1
, the truth value of 0
is False
, so according to the above statement, it results in the first operand itself which is 0
.
One more example, but both the operand’s truth values are True
:
print(2 and 3)
print(3 and 2)
3
2
The statement holds good here too 😊.
In 2 and 3
and 3 and 2
, both the operand’s truth values are True
, so as per the statement if x’s truth value is false, then x, else y, in both cases second operand is the result.
24.5. not
operation on objects.#
not
operation is pretty easy, it just returns opposite of Truth value of object. If the truth value of object is True
, then not <obj>
is False
and vice versa 🙃.
# On boolean.
print(not True)
print(not False)
# On integers.
print(not 1)
print(not 0)
# On list.
print(not [1, 2, 3])
print(not [])
False
True
False
True
False
True
24.6. TLDR about Boolean Operators😬#
or
can be understood as:
def or_(a, b):
if a:
return a
else:
return b
and
can be understood as:
def and_(a, b):
if not a:
return a
else:
return b