Hintor
Python Basics for Programmers

Truthy and Falsy Values

Python for Developers/Python Basics for Programmers/

Truthy and Falsy Values

Python

Lesson explanation

Truthy and Falsy Values


🎯 Learning Objective

After this lesson, you will be able to:

  • Predict the truthiness of False, None, zero, empty text, and non-empty values.
  • Use direct value checks and not for presence or emptiness.
  • Inspect a value explicitly with bool().
  • Keep valid falsy values such as 0 separate from genuinely missing data.

Short Explanation

Python conditions do not require a value to be literally True or False. Many values have an implied truth value.

Common falsy values include:

TEXT
False
None
0
0.0
""

Most other values are truthy, including non-empty strings and non-zero numbers.

This makes direct checks concise:

Python
if api_token:
    print("Token available")

Use not when you want the empty or missing path:

Python
if not api_token:
    print("Token missing")

You can call bool(value) when you want to inspect the truth value directly. But direct truthiness is not always the right test. If 0 is a valid configured value, if not retry_limit: would treat it the same as None. In that case, check specifically for missing data with is None.

💡 Tip: Use truthiness for “does this value have content?” Use is None for “was no value provided?”


Clean Code Examples

1. Check whether text has content

An empty string is falsy, so the fallback branch runs.

Python
release_note = ""

if release_note:
    print("Note ready")
else:
    print("No release note")

Output:

TEXT
No release note

2. Use not for the empty path

not reverses the truth value.

Python
api_token = ""

if not api_token:
    print("Authentication unavailable")

Output:

TEXT
Authentication unavailable

3. Inspect truth values with bool()

bool() makes Python's interpretation visible.

Python
print(bool(0))
print(bool(""))
print(bool("worker"))
print(bool(3))

Output:

TEXT
False
False
True
True

4. Preserve a valid zero

A zero timeout may be intentional, while None means no value was supplied.

Python
timeout_seconds = 0

if timeout_seconds is None:
    print("Timeout: default")
else:
    print(f"Timeout: {timeout_seconds}")

Output:

TEXT
Timeout: 0


⚠️ Common Mistakes

  1. Do not assume falsy always means missing; zero and empty text may be valid values.
  2. Do not write a long comparison when a direct presence check communicates the intent better.
  3. Do not replace a precise is None check with a broad falsy check when the distinction matters.
  4. Do not confuse the string "False" with the boolean False; non-empty text is truthy.
Start Practice

Python for Developers

Truthy and Falsy Values

5 tasks
Lesson progress0 / 5 tasks completed

Tasks

Practice progress

Complete an API-key presence check

View task

Complete the condition so an empty API key takes the missing-key path.

Requirements

  • Keep the existing value and messages unchanged.
  • Fill only the missing condition keyword.

Expected output

API key: missing
Code: PythonTask 1

Complete an API-key presence check

Change the starter code or fill in the blanks, then run it before asking Hintor.

1
2
3
4
5
6
7

Assembled code

Preview of the code that will run

Generated code is hidden by default. Expand it only if you want to inspect what your current blank values produce.

Output / ConsolePreparing Python runtime...

0 / 5 tasks completed

Previous lessonBack to unitBack to track
ValueTruth value
False, NoneFalsy
0, 0.0Falsy
""Falsy
Non-empty textTruthy
Most non-zero numbersTruthy