Python

Lesson 02

Truthy, falsy, and None checks

Distinguish missing values from valid falsy values such as empty strings, zero, and empty collections.

Good Code

display_name.py
def display_name(user):
    if user.nickname is None:
        return user.full_name

    return user.nickname

Bad Code

display_name.py
def display_name(user):
    if user.nickname:
        return user.nickname

    return user.full_name

Review Notes

What to review

Good Code

The good version treats None as the missing case and preserves an intentionally empty nickname.

Bad Code

The bad version treats every falsy value as missing. Empty strings, zero, empty lists, and False can accidentally take the fallback path.

Takeaways

  • Use `is None` when absence is different from an empty value.