0%

Dictionaries and Other Collections · practice

When Values Count as True or False

do not always need an that produces the literal True or False.

You can place another value directly in a condition:

catalog = []

if catalog:
    print("The catalog has items.")
else:
    print("The catalog is empty.")

The empty takes the else path. Python treats some values as false when it needs to make a decision.

Ask how Python sees a value

The bool() shows the result Python uses. Run this example, then replace one of the values and run it again:

Try it

The output is:

False
True
False
True

An empty and an empty list count as false. A non-empty string and a non-empty list count as true.

The terms are:

  • a falsy value counts as False in a condition;

  • a truthy value counts as True in a condition.

These words describe how a value behaves in a condition. They do not change the value’s type. An empty list is still a list.

Common falsy values

These familiar values are falsy:

KindFalsy example
BooleanFalse
no valueNone
number0 or 0.0
string""
list[]
{}
setset()

Most other values of these are truthy. For example, 5, "Python", [0], and {"ready": False} are all truthy.

Look closely at [0]. The number inside the list is falsy, but the list itself is non-empty, so the list is truthy.

What does this print?

values = [0]

if values:
    print("Has an item")
else:
    print("Empty")

Use not for the empty case

You learned that not reverses a Boolean result. It also reverses the Boolean result of a truthy or falsy value:

query = "   "
clean_query = query.strip()

if not clean_query:
    print("Enter a title to search.")

After .strip(), clean_query is "". That empty string is falsy, so not clean_query is True.

This is a common and readable way to check whether a string or collection is empty:

if not catalog:
    print("The catalog is empty.")

Keep explicit checks when the distinction matters

Truthiness groups several values together. That is useful when the rule really means “empty or not empty.” It can be too broad when different falsy values have different meanings.

The search lesson used:

if match is None:
    print("No matching title.")

Keep that explicit check. A search result of None means no record was found. An empty dictionary would be a different value, even though both are falsy.

Prefer the condition that states the program’s actual rule:

  • use if not items when the rule means the collection is empty;

  • use if value is None when the rule means no value was returned;

  • use if score == 0 when zero has a specific meaning worth naming.

Why is if match is None clearer than if not match after a search?

Describe whether a search can begin

The exercise combines the two useful empty checks from this lesson. First check whether the catalog is empty. Then clean the query and check whether any text remains. Return a message instead of printing inside the function.

Task

Complete search_status(catalog, query).

  1. Create clean_query with query.strip().

  2. If catalog is empty, return "The catalog is empty.".

  3. Otherwise, if clean_query is empty, return "Enter a title to search.".

  4. Otherwise return "Ready to search N items for QUERY.", using the catalog length and cleaned query.

Use truthiness with if not catalog and if not clean_query. Do not change the catalog or query.