Data Types
A tour of Python's built-in types and how to convert between them
Python comes with a small, focused set of built-in types. You'll spend 99% of your time juggling these. Each one gets its own page later; this is the map.
Every snippet on this page runs in your browser—no setup required.
Real-world context: Dynamic vs static typing
Python is dynamically typed: the type of a value is checked at runtime, not compile time. This is different from statically typed languages like Java, Go, or TypeScript, where you declare types upfront and the compiler enforces them.
Dynamic typing tradeoffs:
- ✅ Faster to write: no boilerplate type declarations.
- ✅ More flexible: a function can accept any type that supports the operations it uses (duck typing).
- ❌ Errors surface late: typos and type mismatches aren't caught until the code runs.
- ❌ Harder to refactor: tooling can't always infer what type a variable should be.
Type annotations (Python 3.5+) bridge the gap: you can add optional type hints and use tools like mypy to catch type errors before runtime, while keeping Python's runtime flexibility.
def greet(name: str) -> str:
return f"Hello, {name}"In production codebases, type annotations are increasingly standard practice.
The cheat sheet
| Category | Type | Example | Mutable? |
|---|---|---|---|
| Numeric | int | 42, -7, 0b1010 | No |
| Numeric | float | 3.14, 1e-3 | No |
| Numeric | complex | 3+2j | No |
| Text | str | "hello", 'a' | No |
| Boolean | bool | True, False | No |
| Sequence | list | [1, 2, 3] | Yes |
| Sequence | tuple | (1, 2, 3) | No |
| Sequence | range | range(10) | No |
| Mapping | dict | {"a": 1} | Yes |
| Set | set | {1, 2, 3} | Yes |
| Set | frozenset | frozenset({1, 2}) | No |
| Binary | bytes | b"\\x00\\x01" | No |
| Binary | bytearray | bytearray(b"...") | Yes |
| Nothing | NoneType | None | No |
Type hierarchy
Python's type system is built on a hierarchy rooted in object. Every value in Python is an instance of some class, and every class (eventually) inherits from object.
bool is a subclass of int
This is a quirk of Python's design: True and False are instances of bool, which inherits from int. So True == 1 and False == 0 are both true, and you can do arithmetic with booleans: True + True is 2.
Checking the type
type(obj) returns the exact class; isinstance(obj, T) checks the class or any of its parent classes.
isinstance is what you should use in real code, because it respects inheritance:
bool inherits from int
Because bool is a subclass of int, boolean values behave as integers in arithmetic contexts:
print(True + True) # 2
print(False * 10) # 0This is usually harmless, but it can surprise you in functions that expect an integer.
Converting between types
Every built-in type doubles as a converter when you call it as a function.
Mutability matters
Two patterns to remember:
- Immutable types (
int,float,str,tuple,frozenset) cannot be changed after creation. Operations likes.upper()return a new string. - Mutable types (
list,dict,set,bytearray) can be changed in place. Methods likelist.append()modify the object directly.
None
None is the unique value of type NoneType. It represents the absence of a value. Use is None or is not None to check for it (not ==).
Why 'is None' not '== None'?
None is a singleton—there's only one None object in memory. is checks object identity (same object?), while == checks value equality. For singletons like None, True, and False, always use is.
Challenges
A variable line holds the string "42,3.14,hello,True". Split it on commas and produce a list called parsed containing, in order:
- The integer
42 - The float
3.14 - The string
"hello" - The boolean
True
Hard-code the types (you do not need to detect them automatically).
Write a function safe_int(s, default=0) that tries to convert the string s to an integer. If the conversion fails (e.g., s is "abc"), return default instead. Use a try/except block.
Check your understanding
Which of the following Python types is immutable?
list
dict
tuple
set
What does isinstance(True, int) return?
False
True
A TypeError
None
After this code runs, what is the type of x?
x = "42"
x = int(x)
str
int
float
A TypeError
Which statement about None is false?
None is the only value of type NoneType.
You should use is None to check for None.
A function that doesn't return anything raises an error.
None represents the absence of a value.
The next pages drill into each type, starting with numbers.