Is this really true?

Is this really true?#

A rather fundamental concept in programming is based on logical operations. These are baseed on the boolean data type. Python supports this as well. The fundamental values are True and False. Logical operations will evaluate to a result, which is either True or False. Based on this you can decide on how to proceed in your code.

result = True
print(result)
type(result)
True
bool
another_result = False
print(another_result)
type(another_result)
False
bool

So these are the two boolean values in action. Mostly they are the result of comparisons that you perform in your code.

value = 1
some_other_value = 2
print(f"Is value equal to some_other_value?            {value == some_other_value}")
print(f"Is value different from some_other_value?      {value != some_other_value}")
print(f"Is value larger than some_other_value?         {value > some_other_value}")
print(f"Is value lower than some_other_value?          {value < some_other_value}")
print(f"Is value greater or equal to some_other_value? {value >= some_other_value}")
print(f"Is value lower or equal to some_other_value?   {value <= some_other_value}")
Is value equal to some_other_value?            False
Is value different from some_other_value?      True
Is value larger than some_other_value?         False
Is value lower than some_other_value?          True
Is value greater or equal to some_other_value? False
Is value lower or equal to some_other_value?   True

To make the view complete for boolean handling we need a few more operations: and, or and not. These are builtin operators to perform logical operations.

True and False
False
True or False
True
not True
False
not False
True

Based on this we can create the boolean tables for an operation, which are the fundamentals for computing.

and

True

False

True

True

False

False

False

False

or

True

False

True

True

True

False

True

False

Don’t confuse these with the & and | operators. They have a similar meaning, but work bit by bit, or element by element, instead of evaluating the whole value. Let’s check that by looking at some expressions using integer values. Consider these two values.

a = 255
b = 21

Applying the bit or element wise operators for and and or, & and | will result in the following.

c = a & b
c
21
d = a | b
d
255
e = a ^ b
e
234

Not straightforward to understand. Let’s have a look at the bit representations for these two values and the operations. Things will become much clearer then. We can use a special format string to retrieve the bit representation.

print(f"{a=:08b}")
print(f"{b=:08b}")
print(f"{c=:08b}")
a=11111111
b=00010101
c=00010101

Based on our boolean tables above for the and operation, we can now understand, what’s going on. Each bit in c becomes a 1 if both corresponding bit positions in a and b are 1, else the result for this bit is zero. The corresponding value of this bit representation is 21.

print(f"{a=:08b}")
print(f"{b=:08b}")
print(f"{d=:08b}")
a=11111111
b=00010101
d=11111111

We can do the same for the bit-wise or operation. The resulting bit in d will be 1, if either the corresponding bit in a or the one in b is 1. Hence the result is 255.

print(f"{a=:08b}")
print(f"{b=:08b}")
print(f"{e=:08b}")
a=11111111
b=00010101
e=11101010

The result for the last operation is 234 as the resulting bit in e will be 1 only if the values in a and b differ, else the bit will be set to 0. That’s the exclusive or operation. It just makes sense in an element wise comparison. Therfore no equivalent builtin function to be applied to single values, such as an xor completing the and and or functions, exists.

Finally an interesting side note on booleans in Python. Although they look as completely self contained datatypes when quering their type, they’re actually not. To clarify this let’s perform some very simple checks.

True == True
True
True == False
False
False == True
False
False == False
True

Okay. The outcome is exactly as expected here. But let’s play some more.

True == 1
True

Wait. What? True being a boolean has the same value as the integer number? What about False. If this matches, then the following might work as well.

False == 0
True

Okay. So you can perfectly use 1 and 0 instead of True and False within your code. Actually you might find this in some source code around the internet. This is bad practise though! Always use True and False to be explicit as possible. Most often the usage of 1 and 0 originates from bad quality attempts to port algorithms from other programming languages such as C to Python

But it gets even weirder. If True equals to 1 and False to 0. May I even calculate with these booleans?

True + True
2
-2 * True
-2

Looks as though that is the case. Some more consequent weirdness?

-5 * (1 < 2) + (1 < 3)
-4

So much fun!