Python Data Types
Python Data Types: Core Built-In Types and Mutability
Every value in Python belongs to a specific built-in data type that defines what operations you can perform on it. Knowing your data types prevents runtime errors like trying to add a number directly to text.
Python groups its core types into numeric, text, sequence, mapping, set, boolean, and binary categories. You can inspect any object's type at runtime with type() or verify inheritance with isinstance().
Overview of Built-In Python Types
| Category | Type Names | Mutable? | Example |
|---|---|---|---|
| Text | str |
No | "hello" |
| Numeric | int, float, complex |
No | 42, 3.14 |
| Sequence | list, tuple, range |
list only |
[1, 2], (1, 2) |
| Mapping | dict |
Yes | {"id": 1} |
| Set | set, frozenset |
set only |
{1, 2, 3} |
| Boolean | bool |
No | True, False |
| Empty | NoneType |
No | None |
Checking Types Safely in Code
When your function accepts flexible input, prefer isinstance() over type(x) == ... because isinstance() handles subclasses properly.
order_price = 49.95
tags = ["python", "backend"]
profile = {"name": "Tariq", "verified": True}
empty_result = None
print(isinstance(order_price, float)) # True
print(isinstance(tags, list)) # True
print(type(empty_result)) # <class 'NoneType'>
Common Mistakes to Avoid
- Concatenating strings and integers:
"Port: " + 8080raisesTypeError; convert withstr(8080)or use an f-stringf"Port: {8080}". - Comparing mutable and immutable types: Tuples and strings cannot change in place, whereas lists, dicts, and sets mutate in place.
- Testing for
Nonewith==: Always writeif result is None:rather thanif result == None:.
Frequently Asked Questions
What is the difference between mutable and immutable types?
Mutable objects like list and dict allow you to add, remove, or edit items in place. Immutable objects like int, str, and tuple create a new object whenever you change their value.
What does None mean in Python?
None is Python's single null object representing the absence of a value, commonly returned by functions that have no explicit return statement.
Are Python booleans related to integers?
Yes. bool is a subclass of int, so True equals 1 and False equals 0 in numeric contexts.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.