Deciding
Worked examples
Read the claim, then run it and check the machine agrees. One at a time — nothing here is taken on trust.
def bucket(bp):
if bp > 25:
return "big up"
elif bp >= 0:
return "up"
else:
return "down"
print(bucket(50))
print(bucket(10))
print(bucket(-5))
Same three branches, three different paths through them. 50 > 25 is true so
the first branch wins — 'big up'. 10 is not over 25 but it is at least
0, so the elif catches it — 'up'. -5 fails both, so else fires —
'down'. Notice 10 lands in 'up', not 'big up': the boundary is 25, and
the first matching branch always wins.
print(bool([])) # empty list
print(bool("N/A")) # a non-empty string
print(bool(0)) # the number zero
print(bool([0])) # a LIST holding zero
print(bool(-1)) # a negative number
Empty list, empty-nothing: False. A string with characters in it: True.
The number 0: False. But [0] — a list with one element — is True,
because the list isn't empty; what's inside it doesn't matter. And -1 is
truthy: falsy means zero or empty, never negative.
name = "" print(name or "N/A") print(0 and 5) print(0 or 5)
name is the empty string, which is falsy, so or skips past it and returns
the fallback 'N/A'. Below it: and returns 0 (it stops at the first
falsy), and or returns 5 (the first truthy). These operators hand you back
an actual operand — that's exactly why name or "N/A" works as a default.
Check the concept
One question at a time. Unsure? Revisit the lecture, then answer.
The challenge
Pass the quiz to unlock the challenge — your code will still be waiting here.