Have a go at it. The editor and the docs panel are open, and your code is saved as you type. Running it needs a free account — you’ll come back to exactly what you wrote.
Comparing Values: == and is
Two different questions, two different operators:
== asks is this the same value?is asks is this the same object in memory?Almost always you want ==.
a = [1, 2] b = [1, 2] a == b # True same contents a is b # False two separate lists that happen to match
is has exactly one common correct use: comparing against None.
if value is None: # correct, and what everyone writes if value == None: # works, but not idiomatic
There is a related trap. if not value: is not the same as if value is None:, because 0, "" and [] are all falsy but are not None:
value = 0 if not value: # True! even though a value was supplied if value is None: # False which is what you meant
That distinction matters the moment a real value can legitimately be zero, which in data work is constantly.
Your task:
Write describe(value) that returns:
"missing" when the value is None"empty" when it is an empty list"present" for anything else, including 0 and an empty stringExample Tests
None is missing
Input: {"value":null}
Expected: "missing"
An empty list is empty
Input: {"value":[]}
Expected: "empty"
Zero is a real value, so it is present, not missing
Input: {"value":0}
Expected: "present"
def describe(value):
"""
Classify a value as missing, empty, or present.
Args:
value: anything, possibly None
Returns:
"missing" for None, "empty" for [], "present" otherwise.
"""
# YOUR CODE HERE
passRun your code to see results
⌘↵ runs against the visible tests