Introduction to Datatypes in Python
Datatypes are the foundation of any programming language — they define what kind of data can be stored, how it behaves, and what operations are possible on it. Python is dynamically typed, meaning you don’t declare types explicitly; the interpreter infers them at runtime. This flexibility makes Python fast to write and read, but understanding datatypes deeply is key to writing efficient, bug-free, and performant code.
In 2026, Python’s core datatypes remain largely the same as in earlier versions, but modern usage patterns have evolved significantly with tools like Polars, Dask, NumPy, Pydantic, dataclasses, and type hints (mypy, pyright). This guide covers all major built-in datatypes with practical, up-to-date examples, real-world use cases (especially in data science), and best practices for 2026-era Python development.
1. Numeric Types
- int — arbitrary-precision integers (no size limit in Python 3+)
- float — double-precision floating-point numbers (IEEE 754)
- complex — complex numbers with real and imaginary parts
- bool — subclass of int, only True (1) and False (0)
# int — unlimited size
huge = 2 ** 1000
print(type(huge)) #
# float — careful with precision
print(0.1 + 0.2) # 0.30000000000000004 (floating-point artifact)
print(1.0 == 1) # True (bool is subclass of int)
# complex
c = 3 + 4j
print(c.real, c.imag) # 3.0 4.0
print(abs(c)) # 5.0 (magnitude)
2. Textual Type: str
Immutable sequence of Unicode characters. Supports all Unicode (emojis, math symbols, etc.).
s = "Hello ? Python 3.12+"
print(len(s)) # 20 (counts code points)
print(s[7]) # ? (emoji is one character)
print(s.upper()) # HELLO ? PYTHON 3.12+
print(f"Unicode code point of ?: {ord('?')}") # 127757
3. Sequence Types
- list — mutable, ordered, heterogeneous
- tuple — immutable, ordered, heterogeneous
- range — immutable, memory-efficient sequence of integers
- bytes — immutable sequence of bytes
- bytearray — mutable sequence of bytes
lst = [1, "two", 3.0, True]
tup = (1, "two", 3.0, True)
rng = range(0, 10, 2) # 0, 2, 4, 6, 8
print(list(rng)) # [0, 2, 4, 6, 8]
print(tuple("abc")) # ('a', 'b', 'c')
4. Mapping Type: dict
Mutable, ordered (insertion order since Python 3.7), key-value pairs. Keys must be hashable.
d = {"name": "Alice", "age": 30, "active": True}
print(d["name"]) # Alice
# Modern dict unpacking & merge (Python 3.9+)
defaults = {"timeout": 30}
config = {"host": "localhost", **defaults, "port": 8080}
print(config) # {'host': 'localhost', 'timeout': 30, 'port': 8080}
5. Set Types
- set — mutable, unordered, unique elements
- frozenset — immutable version (hashable)
s = {1, 2, 2, 3} # {1, 2, 3}
fs = frozenset([4, 5, 5]) # frozenset({4, 5})
print(1 in s) # True (O(1) lookup)
print(s | {3, 4}) # {1, 2, 3, 4} union
6. NoneType: None
The singleton null object — represents absence of value.
result = None
if result is None:
print("No value returned")
7. Binary Types
- bytes — immutable byte sequence
- bytearray — mutable byte sequence
- memoryview — zero-copy buffer view
b = b"hello"
ba = bytearray(b"world")
mv = memoryview(ba)
mv[0] = ord('W') # mutable via memoryview
print(ba) # bytearray(b'World')
Modern Python Datatypes & Patterns (2026)
- dataclasses — simple classes with auto-generated __init__, __repr__, etc.
- TypedDict / NamedTuple — type-safe dictionaries and tuples
- Pydantic models — runtime-validated data structures
- Polars Series / DataFrame — columnar data (replaces pandas in many cases)
- Dask DataFrame / Array — distributed computing
- NumPy arrays — n-dimensional numeric arrays
from dataclasses import dataclass
@dataclass
class Quake:
mag: float
place: str
depth: float = 0.0
q = Quake(7.5, "Alaska")
print(q) # Quake(mag=7.5, place='Alaska', depth=0.0)
Conclusion
Understanding Python’s datatypes — and how they behave in modern libraries like Polars, Dask, NumPy, and Pydantic — is the foundation of writing efficient, readable, and maintainable code. Choose the right type for the job: use tuples for fixed records, sets for uniqueness, dicts for mappings, lists for mutable sequences, and specialized containers (DataFrame, array) for data analysis. With type hints and modern tools, Python’s dynamic typing becomes safer and more powerful than ever.
Keep experimenting with datatypes — they unlock Python’s full potential in data science, web development, automation, and beyond!