Python If/Else and Booleans: Examples and Practice Questions
Python If/Else and Booleans: Examples and Practice Questions¶
These are the solutions for the exercises in Boolean Expressions in Python: Beginner to Expert. For the runnable exercises without the solutions, this notebook.
For each of the following questions, give the best answer. You can assume all code is indented/aligned correctly at the left even if it appears indented slightly in the question text.
1. The following Python code contains one or more errors. How would you correct them?
water_level = 5
if water_level > 3
print("Open flood gates")
else
print("Normal operations")
Add a colon at end of if line and else line
water_level = 5
if water_level > 3:
print("Open flood gates")
else:
print("Normal operations")
Open flood gates
2. Which of the following is not a boolean expression, and why?
5 < 2
2 < 3
3.5 <= 7
3 = 7
my_variable is not None
Number 4 is not a boolean expression, it should be double equals, not single. Single equals is used for assignment, as in message = “Hello”. Some others evaluate to False, that’s OK, they’re still valid boolean expressions. Number 5 might throw an exception if my_variable is not defined, but it’s valid otherwise.
3. What does the following print?
noise_level = 2
heat_output_percent = 98
if noise_level > 4 or heat_output_percent < 95:
print("Call technician")
else:
print("Normal operation")
Normal operation
4. What does the following print:
to_be = True
print(to_be and not to_be)
print(to_be or not to_be)
False
True
5. What does the following print:
water_level = 3
time_of_day = "PM"
heat_ouput_percent = 75
print(water_level > 5 and time_of_day == "PM" or heat_output_percent < 90)
False
6. The following code works OK, but what if anything is wrong with it?
if (3 > 10) or (4.5 > 9):
print("Gotcha!")
What is the value of obscure after the following code runs?
obscure = True and False or False or True
True. The and
expression is evaluated first, and evaluates to False. But because the final term of the or
clauses is True, obscure evaluates to true.
7. How might the following code be improved:
rebounds = None
if rebounds != None:
print("Got some rebounds!")
else:
print("No rebounds")
Although this code works, the recommended way to write the if clause in this case would be:
if rebounds is not None:
8. In the following code, which expressions are evaluated?
light_level = 10
decibel_level = 30
result = decibel_level > 50 or light_level == 10
A) decibel_level > 50
B) light_level == 10
C) Both A and B
D) Neither A nor B
C. Both are evaluated. The first comparison evaluates to False, but since it is an “or” statement, evaluation needs to continue to see if the second comparison is true.
9. Which of the following statements is false? There may be more than one.
A) An else clause must be preceded by an if clause. B) An if clause must be followed by an else clause. C) Usually, a Boolean expression contains one or more comparison operations. D) Boolean expressions can appear outside of an if statement. E) Boolean expressions must include a Boolean literal (i.e. one of the the keywords "True" or "False")
B and E are false. B is false because if clauses do not need to be followed by either elif or else. Those clauses are optional. E is false because Boolean expressions can have a Boolean literal most don’t – they simply contain comparison operations.
10. What is the error in the following code? How would you fix it?
water_level = 5
if water_level and water_level < 3:
print("Normal operations")
else if water_level >= 3 and water_level <= 4.5:
print("Call City Manager")
else:
print("Open flood gates and call City Manager")
The code “else if” should be replaced with “elif”, as below:
water_level = 5
if water_level and water_level < 3:
print("Normal operations")
elif water_level >= 3 and water_level <= 4.5:
print("Call City Manager")
else:
print("Open flood gates and call City Manager")
Open flood gates and call City Manager