Repaso
EspaƱol
PythonCorePY-57

What does this print?

items = []
if items:
    print("has items")
else:
    print("empty")

Options

  1. has items
  2. empty (Correct option)
  3. An error
  4. 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.

Key terms

Practice this question in Repaso