What does this print?
items = []
if items:
print("has items")
else:
print("empty")Options
- has items
- empty (Correct option)
- An error
- None
Answer
empty
Why
In an if, Python treats some values as False ('falsy'): an empty list [], an empty string "", 0, None and an empty dict {}.
Everything else is 'truthy'. So if items: is the short way to check that a list is not empty.
Remember
Empty things, 0 and None are falsy.