Data Types โ Complete Guide
Python has rich built-in data types โ from numbers and strings to lists, tuples, dictionaries, and sets. Every value in Python is an object with a type.
Python's Built-in Data Types:
Numeric Types:
| Type | Example | Description |
|------|---------|-------------|
| int | 42, -17, 0 | Integers of unlimited size |
| float | 3.14, -0.001, 2.0 | 64-bit floating point (IEEE 754) |
| complex | 3+4j, 1j | Complex numbers with real and imaginary parts |
| bool | True, False | Boolean (subclass of int: True=1, False=0) |
Sequence Types:
| Type | Example | Mutable? |
|------|---------|----------|
| str | "hello", 'world' | โ Immutable |
| list | [1, 2, 3] | โ
Mutable |
| tuple | (1, 2, 3) | โ Immutable |
| range | range(10) | โ Immutable |
Mapping & Set Types:
| Type | Example | Description |
|------|---------|-------------|
| dict | {"a": 1, "b": 2} | Key-value pairs, ordered (3.7+) |
| set | {1, 2, 3} | Unordered unique elements |
| frozenset | frozenset({1, 2}) | Immutable set |
Special Types:
NoneTypeโNonerepresents the absence of a valuebytesโ Immutable sequence of bytes:b"hello"bytearrayโ Mutable sequence of bytes
Key Insight โ Mutable vs Immutable:
- Immutable (int, float, str, tuple, frozenset): Cannot be changed after creation. Operations create NEW objects.
- Mutable (list, dict, set, bytearray): Can be modified in place.
This distinction is critical for understanding function arguments, dictionary keys, and shared references.
# Check type
type(value)
# Check if instance of a type
isinstance(value, int)
isinstance(value, (int, float)) # Multiple types
# Common type constructors
int(), float(), str(), bool(), list(), tuple(), dict(), set()