Boolean variables are like numeric variables. It’s possible to assign expressions, perform operations, and print their values.
To check if two values are equal we can use
a = 10 b = 20 / 2 print(a == b) # True c = a == b print(c) # True d = 3 print(a == d) # False
Here the expression
a == bresults in
10is equal to
20 / 2. We can even assign the result of
a == bcheck to another variable
cand print its value.
In Python, the value of
Truecan be interpreted as
1and the value of
Falsecan be interpreted as
0. So, we can even perform additions and multiplications with boolean values:
print(True + True) # 2 print(True * 10) # 10 print(True + False - 3) # -2 print(10 * False) # 0
This allows to treat boolean values exactly like numeric values and perform computations.
As you already know how to play with boolean variables, try to solve this challenge with pen and paper (or even in your head if you’re a pro 😎).
What will be the output of the following program?
a = 7 b = 2 c = a == b d = a == 14 / 2 e = a * d * 10 f = a * c print(c, d, f)