Skip to content

None & Truthiness

None: Python’s single “nothing” value

Section titled “None: Python’s single “nothing” value”

TypeScript has two “absence” values — null and undefined — and a whole linting rule (strictNullChecks) to manage them. Python has one: None. It is both the “no value” sentinel AND what functions return implicitly when they do not return anything explicitly.

You compare against None with is (not ==) — more on that below.

TypeScript
// TypeScript
let user: string | null = null;
let config: string | undefined = undefined;
// Nullish coalescing
const name = user ?? "Guest";
// Optional chaining
const len = user?.length ?? 0;
// Type narrowing
if (user !== null && user !== undefined) {
console.log(user.toUpperCase());
}
Python
# Python
user: str | None = None # str | None = Optional[str]
# Fallback with "or"
name = user or "Guest"
# Conditional expression (ternary)
length = len(user) if user is not None else 0
# Type narrowing
if user is not None:
print(user.upper())

Python’s falsy rules are similar to JavaScript’s but stricter and more consistent. Any empty container is falsy; any non-zero number is truthy.

ValueFalsy in Python?Falsy in JS/TS?
NoneYesnull / undefined — Yes
0YesYes
0.0YesYes
"" (empty string)YesYes
[] (empty list)YesNo — [] is truthy in JS
{} (empty dict)YesNo — {} is truthy in JS
set() (empty set)YesN/A
() (empty tuple)YesN/A

Notice: empty arrays and objects are truthy in JavaScript but falsy in Python. This catches a lot of TS developers off guard.

TypeScript
// TypeScript
console.log(Boolean([])); // true — empty array is truthy!
console.log(Boolean({})); // true — empty object is truthy!
console.log(Boolean(0)); // false
console.log(Boolean("")); // false
console.log(Boolean(null)); // false
if ([]) console.log("truthy"); // prints!
Python
# Python
print(bool([])) # False — empty list is FALSY
print(bool({})) # False — empty dict is FALSY
print(bool(0)) # False
print(bool("")) # False
print(bool(None)) # False
if []:
print("truthy") # does NOT print
else:
print("falsy") # prints
# Idiomatic: guard against empty containers
items = []
if not items:
print("No items found")

In Python, is checks identity (same object in memory). == checks equality (same value). For None, always use is — there is only one None object in the Python process, so identity is the correct check.

TypeScript
// TypeScript — use strict equality
function process(data: string | null | undefined): void {
if (data === null || data === undefined) {
console.log("No data");
return;
}
console.log(data.toUpperCase());
}
Python
# Python — use "is None" / "is not None"
def process(data: str | None) -> None:
if data is None:
print("No data")
return
print(data.upper())
# Common pattern: default if None
def greet(name: str | None = None) -> str:
name = name or "World" # replaces None (or falsy) with default
return f"Hello, {name}!"
print(greet()) # Hello, World!
print(greet("Alice")) # Hello, Alice!
# None checks
x = None
print(x is None) # True
print(x is not None) # False
# Truthiness
print(bool([])) # False — empty list
print(bool([1])) # True — non-empty list
print(bool({})) # False — empty dict
print(bool({"a": 1})) # True
print(bool("")) # False — empty string
print(bool("hi")) # True
print(bool(0)) # False
print(bool(1)) # True
# None in function return
def get_user(user_id: int):
if user_id == 1:
return {"name": "Alice"}
return None # implicit in Python, but explicit is fine
user = get_user(99)
if user is None:
print("User not found")
user2 = get_user(1)
if user2 is not None:
print(f"Found: {user2['name']}")
Which Python value is equivalent to both `null` AND `undefined` in TypeScript?
What does `bool([])` return in Python?
Why should you use `x is None` instead of `x == None` in Python?
What does an empty dict `{}` evaluate to in Python truthiness context?