Exercises
Booleans
3 tasks. Write the code, press Check, and the page runs it against a real Python interpreter.
Exercise 1Passed
Print a message only when the list is empty, using truthiness rather than len().
Python
items = []
# print "nothing here" when items is empty
An empty list is falsy, so 'if not items:' reads well.
items = []
if not items:
print("nothing here")Exercise 2Passed
name is empty. Print Anonymous instead, in one line, using or.
Python
name = ""
# print name, or Anonymous when it is empty
or hands back the first truthy operand.
name = ""
print(name or "Anonymous")Exercise 3Passed
Count how many scores are above 60 and print the number.
Python
scores = [45, 80, 62, 95]
# print how many are above 60
A comparison is worth 1 when true, so sum() over them counts the matches.
scores = [45, 80, 62, 95]
print(sum(score > 60 for score in scores))