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.

boolInput1 and boolInput2 == False doesn’t do what you think. The == binds more tightly than the and, so you’re testing “is boolInput1 (truthy), and is boolInput2 equal to False”, when you want “is boolInput1 False and boolInput2 False too?”, which would be expressed boolInput1 == False and boolInput2 == False or more Pythonically, not boolInput1 and not boolInput2.

Really, you’re making this harder than it needs to be. All of your code could simplify to just:

print(not boolInput1 and not boolInput2)

or extracting the not if you prefer it:

print(not (boolInput1 or boolInput2))

No if, elif, else or any other blocks required.

Generally speaking, explicitly comparing to True or False is not Pythonic; just use implicit “truthiness” testing to work with any types. Since you need not here anyway, the end result will always be True or False, even if the inputs aren’t booleans at all, where directly comparing to True or False will make inputs like 2, None, or [] behave differently from the way they traditionally behave in “truthiness testing” (they’d be truthy, falsy and falsy respectively).

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 a
else:
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 a
else:
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()
True
false_func() or true_func() false_func()
true_func() True
true_func() and false_func() true_func()
false_func() False
false_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 = True
y = True
z = x and y # z = True
x = True
y = False
z = x and y # z = False
x = False
y = True
z = x and y # z = False
x = False
y = False
z = x and y # z = False
x = 1
y = 1
z = x and y # z = y, so z = 1, see and and or are not guaranteed to be a boolean
x = 0
y = 1
z = x and y # z = x, so z = 0 (see above)
x = 1
y = 0
z = x and y # z = y, so z = 0 (see above)
x = 0
y = 0
z = 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 = True
y = True
z = x or y # z = True
x = True
y = False
z = x or y # z = True
x = False
y = True
z = x or y # z = True
x = False
y = False
z = x or y # z = False
x = 1
y = 1
z = x or y # z = x, so z = 1, see and and or are not guaranteed to be a boolean
x = 1
y = 0
z = x or y # z = x, so z = 1 (see above)
x = 0
y = 1
z = x or y # z = y, so z = 1 (see above)
x = 0
y = 0
z = 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 = True
y = not x # y = False
x = False
y = not x # y = True

Learn More Here

LEAVE A REPLY

Please enter your comment!
Please enter your name here