Hands On Exercise

We'll cover the following

Congratulations on completing the lesson on Python booleans. Test your knowledge through these short snippets and get one step closer to mastering Python!

Exercise 1

Suppose we have two variables, a and b, with the values 25 and 12, respectively. Before clicking on the solution button, think: What will the following expressions evaluate to?

Press + to interact
a = 25
b = 12
print(a < b)
print(a >= b)
print(a == b)

Exercise 2

We have three variables named clouds, sunshine, and rain. Define rain such that it’s True if there are clouds and no sunshine. Click on the solution button to get your answer.

Press + to interact
sunshine = False
clouds = True
rain = (not sunshine and clouds)
print(rain)

The exercise returns True. It first checks if there is no sunshine using not sunshine, which is True as the inverse (not) of sunshine = False is True. Then it checks if there are any clouds, which is also True. When both conditions on each side of an and operator are True, the final result is also True,

Exercise 3

Use the bool() method to evaluate whether my_list = [0] will be True or False, why do you think the answer is what it is? Click on the solution button to get your answer.

Press + to interact
my_list = [0]
print(bool(my_list))

my_list evaluates to True since it is not an empty collection, even if it just consists of zero.

On the other hand, my_list = [] would have evaluated to False.