Truthiness
- Python course
- Beginner
- Lesson 39/116
- 4 min
Learn values considered true/false.
Truthiness
Python evaluates every value as bool. Falsy values: 0, 0.0, , [], None. Others are truthy.
name = "Alex"
if name: # truthy
print("Has a name")This gives a very short way to say "if not empty". if text: is True when text is non-empty.
Checking the opposite with not
not flips the truthiness of a value. not name is True when name is empty.
name = ""
if not name:
print("No name")This is a short way to say "if empty". With if not text: you can check for an empty string, empty list or None.
Practice Print with truthiness
Assign name = "Alex". Write if name: print("Has a name").
Starter code
# Use truthinessSample solution
name = "Alex"
if name:
print("Has a name")Expected output
Has a name
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which is falsy?
- "text"
- 0 Correct answer
- 1
- [1, 2]
Explanation: 0 is a falsy value.
2 Empty string check
name = "Alex"
if ____:
print("Has a name")
name = "Alex"
if name:
print("Has a name")
Explanation: if name is True for a non-empty string (truthy).
3 Match the values
| Term | Meaning |
|---|---|
0 |
Zero (falsy) |
"text" |
String (truthy) |
[] |
Empty list |
None |
None value |