Data types
A data type is the kind of value a name refers to: integer, string, list, function, and so on. In Python every value is an object with a type, and that type decides which operations are legal.
Detailed Explanation
Python does not store a type on the variable. It stores a type on the object. score = 12 binds the name score to an int object. Later score = "twelve" rebinds the same name to a str. The name never changed type because the name never had a type. The objects did.
Every object carries a type pointer. type(12) is int. type("twelve") is str. The type is itself an object (int is an instance of type), which is why you can pass types around, store them, and even create new ones at runtime. That is the same mechanism classes use.
Built-in types fall into a few groups you will use every day:
| Group | Types | Typical use |
|---|---|---|
| Numbers | int, float, complex, bool | Counts, measurements, flags |
| Text and bytes | str, bytes, bytearray | Human text vs raw binary |
| Sequences | list, tuple, range | Ordered collections |
| Sets | set, frozenset | Unique membership |
| Mappings | dict | Key to value lookup |
| Callables | function, method, type | Things you can call with () |
bool is a subclass of int, so True == 1 and True + True == 2. That is a language rule, not a curiosity. It is why a boolean can sneak into arithmetic without an error.
Python distinguishes mutable types from immutable ones. int, float, str, bytes, tuple, and frozenset cannot change in place. list, dict, set, and bytearray can. Mutability decides whether two names sharing an object will surprise you, and whether the object can be a dictionary key.
A type is not only a label. It is a collection of operations. Adding two ints produces another int. Adding two strs concatenates. Adding an int to a str raises TypeError. The interpreter looks up the operation on the type of the left operand first (__add__), then the right (__radd__). If neither knows how to do it, the program stops.
You will meet two other layers of "type" later. Type hints describe intended types for humans and checkers; they do not change runtime behaviour. Abstract types such as collections.abc.Sequence describe a protocol: anything that supports len and indexing can pass as a sequence even if it is not a list.
Reach for the built-in that matches the question you are asking. A count is an int. A lookup is a dict. A unique set of ids is a set. Using a list for membership tests works until the list is long, and then the type choice becomes the bug.
Code Examples
# The name has no type. The object does.
score = 12
print(type(score)) # <class 'int'>
score = "twelve"
print(type(score)) # <class 'str'>
# bool is a subclass of int, so these are legal.
print(True == 1) # True
print(isinstance(True, int)) # True
print(True + True) # 2
# Operations are looked up on the type.
print(3 + 4) # 7
print("3" + "4") # 34
# print(3 + "4") # TypeError: unsupported operand type(s)# Mutability is a property of the type, not of the name.
sku = "A-100"
# sku[0] = "B" # TypeError: 'str' object does not support item assignment
codes = ["A-100", "B-200"]
codes[0] = "B-100" # lists can change in place
print(codes) # ['B-100', 'B-200']
print(int.__name__, list.__name__)
print(isinstance(codes, list)) # True
print(isinstance(sku, (str, bytes))) # True, either type is acceptableCommon Gotchas
`type(x)` is exact, `isinstance` walks the hierarchy
`type(True) is int` is `False` because the exact type is `bool`. `isinstance(True, int)` is `True`. Prefer `isinstance` unless you truly need an exact match.
Mixing types does not coerce silently
`"3" + 4` is a `TypeError`, not `"34"`. Convert explicitly with `int`, `str`, or `float`.
`bool` is an `int`
a function that accepts numbers will also accept `True` and `False`. If that is wrong for your API, check `type(flag) is bool` or reject the value.
Type hints are not runtime types
`def area(w: int) -> int` still accepts a string unless a checker or a validator runs. The annotation is documentation until you enforce it.