5.1 Introduction to Data Types
What data types are, why choosing the right one matters, how dynamic typing works, and real-world examples of Python values mapped to their types.
The built-in data types covered in this chapter, grouped by category.
What Are Data Types?
What Is It?
A data type classifies what kind of value something is (a number, text, a collection) and, in turn, what operations are valid on it.
Why Does It Matter?
The type determines behavior — you can add two ints, but adding an int to a list raises a TypeError.
How Is It Used?
Every value in Python has exactly one type at any given moment, discoverable with type().
>>> type(42)
<class 'int'>
>>> type("hello")
<class 'str'>
Why Data Types Matter
Choosing the right type affects correctness (can this value be negative? can it have duplicates?), performance (list vs set membership testing), and memory usage. A large part of writing good Python is picking the type that matches your data’s real-world shape.
Dynamic Typing
What Is It?
A variable has no fixed type — its type is simply whatever value it currently holds, and reassignment can change that type freely. This was introduced in 4.8 Variables and is worth re-grounding here since it’s foundational to how types work in Python.
>>> x = 10
>>> type(x)
<class 'int'>
>>> x = "now a string" # same name, completely different type -- perfectly legal
>>> type(x)
<class 'str'>
Real-World Examples
- A user’s age →
int - A server’s hostname →
str - A list of IP addresses to block →
listorset - An API response body →
dict(parsed from JSON) - A file’s raw contents →
bytes
Quick Interview Answer
“A data type classifies what kind of value something is and what operations are valid on it — Python discovers a value’s type at runtime with
type(), since it’s dynamically typed: a variable’s type is whatever value it currently holds, and reassigning it to a different type is perfectly legal. Picking the right type up front affects correctness, performance, and memory usage.”
Common Mistakes
- Assuming a variable’s type is fixed once assigned — reassignment to a different type is silent and completely legal in Python.
- Not checking
type()orisinstance()before an operation, then getting aTypeErrorat runtime instead of catching a bad assumption early.
Add More Questions to This Guide
Know a question that should be here? Share it and help the community!
Open Google Form