Python Basics — Booleans & None
Booleans & None
Booleans and None are two of Python’s simplest but most important data types. Booleans represent truth values (True or False), while None represents the absence of a value.
Boolean Type (bool)
A boolean can only have two values: True or False.
is_active = True
is_deleted = False
print(type(is_active)) # <class 'bool'>
print(type(is_deleted)) # <class 'bool'>
Important: True and False must be capitalized. true or false will cause a NameError.
Boolean Operations
Python supports three boolean operators: and, or, and not.
a = True
b = False
print(a and b) # False — both must be True
print(a or b) # True — at least one must be True
print(not a) # False — reverses the value
print(not b) # True
Truth Tables
and — returns True only if both are True:
| A | B | A and B |
|---|---|---|
| True | True | True |
| True | False | False |
| False | True | False |
| False | False | False |
or — returns True if at least one is True:
| A | B | A or B |
|---|---|---|
| True | True | True |
| True | False | True |
| False | True | True |
| False | False | False |
not — reverses the value:
| A | not A |
|---|---|
| True | False |
| False | True |
Comparison Operators Return Booleans
print(5 > 3) # True
print(10 == 10) # True
print(7 != 7) # False
print(3 >= 5) # False
print("a" < "b") # True (lexicographic comparison)
Truthy and Falsy Values
In Python, every value has a boolean equivalent. When used in a condition, some values are treated as False (falsy) and everything else is treated as True (truthy).
Falsy Values (evaluate to False)
| Value | Type | Example |
|---|---|---|
False |
bool | bool(False) → False |
0 |
int | bool(0) → False |
0.0 |
float | bool(0.0) → False |
0j |
complex | bool(0j) → False |
"" |
str (empty) | bool("") → False |
[] |
list (empty) | bool([]) → False |
() |
tuple (empty) | bool(()) → False |
{} |
dict (empty) | bool({}) → False |
set() |
set (empty) | bool(set()) → False |
None |
NoneType | bool(None) → False |
Truthy Values (evaluate to True)
Everything that is NOT in the falsy list is truthy:
print(bool(1)) # True
print(bool(-1)) # True (any non-zero number)
print(bool("hello")) # True (any non-empty string)
print(bool([1, 2])) # True (any non-empty list)
print(bool(" ")) # True (space is not empty!)
Using Truthiness in Conditions
name = "Alice"
if name: # Truthy — non-empty string
print(f"Hello, {name}!")
items = []
if not items: # Falsy — empty list
print("Your cart is empty.")
Output:
Hello, Alice!
Your cart is empty.
The bool() Function
Convert any value to a boolean:
print(bool(42)) # True
print(bool(0)) # False
print(bool("hello")) # True
print(bool("")) # False
print(bool(None)) # False
print(bool([1])) # True
print(bool([])) # False
Booleans Are Integers
In Python, True is actually 1 and False is 0:
print(True + True) # 2
print(True + False) # 1
print(True * 10) # 10
print(False * 10) # 0
print(isinstance(True, int)) # True — bool is a subclass of int
This is useful for counting True values:
results = [True, False, True, True, False]
print(sum(results)) # 3 (counts True values)
The None Type
None represents the absence of a value. It’s Python’s version of “null” or “nothing.”
result = None
print(result) # None
print(type(result)) # <class 'NoneType'>
Common Uses of None
# 1. Default variable value
user = None # Will be set later
# 2. Function that doesn't return anything
def greet(name):
print(f"Hello, {name}!")
result = greet("Alice") # Hello, Alice!
print(result) # None
# 3. Default parameter value
def find_user(user_id, database=None):
if database is None:
database = get_default_db()
# ...
Checking for None
Always use is (not ==) to check for None:
x = None
# ✅ Correct — use "is"
if x is None:
print("x has no value")
# ✅ Also correct — "is not"
if x is not None:
print("x has a value")
# ❌ Works but not recommended
if x == None:
print("This works but is not Pythonic")
Why is instead of ==? The is operator checks identity (same object in memory), which is exactly what we want for None. The == operator checks equality, which can be overridden by custom classes and produce unexpected results.
None vs False vs 0 vs “”
These are all falsy, but they are different values:
print(None == False) # False
print(None == 0) # False
print(None == "") # False
print(False == 0) # True (bool is subclass of int)
print(None is None) # True (there's only one None)
print(False is False) # True
Common Mistakes
| Mistake | Example | Fix |
|---|---|---|
Using == None |
if x == None: |
Use if x is None: |
| Lowercase boolean | true, false |
Use True, False |
Confusing None with "" |
They’re different types | Check type if unsure |
| Assuming 0 means None | 0 is None → False |
They are different values |
Summary
- Booleans have two values:
TrueandFalse - Use
and,or,notfor boolean operations - Every Python value has a truthy or falsy equivalent
- Falsy values:
False,0,0.0,"",[],(),{},set(),None - None represents the absence of a value (like null in other languages)
- Always use
is Noneandis not Noneinstead of== None - Booleans are integers:
Trueis1,Falseis0
Next up: Type Conversion →