In Python’s if and while statements, you can evaluate not only the bool type (True/False) but also any object such as numbers, strings, and lists.
Python has a basic rule: “Empty objects or zero are regarded as False, and everything else is regarded as True.”
This article explains how major data types are evaluated in conditional expressions using a table and code examples.
Truth Value Evaluation List by Data Type
Here is a summary of what is considered False and what is considered True for each data type.
| Data Type | Type Name | Evaluated as False | Evaluated as True |
| Boolean | bool | False | True |
| Integer | int | 0 | All integers except 0 (e.g., 1, -5) |
| Float | float | 0.0 | All numbers except 0.0 (e.g., 0.1, -3.5) |
| String | str | "" (Empty string) | Strings with 1+ characters (e.g., "a", "0", "False") |
| List | list | [] (Empty list) | Lists with elements (e.g., [0], ["a"]) |
| Tuple | tuple | () (Empty tuple) | Tuples with elements (e.g., (0,)) |
| Dictionary | dict | {} (Empty dict) | Dictionaries with elements (e.g., {"k": 1}) |
| Set | set | set() (Empty set) | Sets with elements (e.g., {1}) |
| None | NoneType | None | (None. Always False) |
Specific Code Examples
Here are code examples showing how each type is actually evaluated in an if statement.
1. Numbers (int, float)
Only 0 or 0.0 is False. Everything else (including negative numbers) is True.
# Integer (int)
zero_value = 0
if zero_value:
print("0 is True")
else:
print("0 is False") # This runs
integer_value = 100
if integer_value:
print(f"{integer_value} is True")
# Float (float)
float_zero = 0.0
if float_zero:
print("0.0 is True")
else:
print("0.0 is False") # This runs
float_value = 0.5
if float_value:
print(f"{float_value} is True")
2. Strings (str)
If the length is 0 (empty string), it is False. If there is even one character (including a space), it is True. Note: The strings "0" and "False" contain characters, so they are True.
# Empty string
empty_text = ""
if empty_text:
print("Empty string is True")
else:
print("Empty string is False") # This runs
# String
message = "Python"
if message:
print(f"'{message}' is True")
3. Collections (list, tuple, dict, set)
Container objects like lists and dictionaries are False if they are empty, and True if they contain at least one element.
# List (list)
empty_list = []
if empty_list:
print("Empty list is True")
else:
print("Empty list is False") # This runs
stock_list = ["apple", "orange"]
if stock_list:
print(f"List {stock_list} is True")
# Tuple (tuple)
empty_tuple = ()
if empty_tuple:
pass
else:
print("Empty tuple is False") # This runs
# Dictionary (dict)
user_data = {"id": 101}
if user_data:
print(f"Dictionary {user_data} is True")
# Set (set)
unique_numbers = {1, 3, 5}
if unique_numbers:
print(f"Set {unique_numbers} is True")
4. None (NoneType)
None is a special type representing “no value” and is always evaluated as False.
# NoneType
nothing = None
if nothing:
print("None is True")
else:
print("None is False") # This runs
Summary
The rules for truth value testing in Python are simple.
- Becomes False:
0,0.0,"",[],(),{},set(),None,False - Becomes True: Everything else
Using this property allows you to write if my_list: instead of if len(my_list) > 0:, making your code more concise.
