Course Content
Python Essentials for AI Engineer
6 sections · 48 lessons
What are Python’s built-in data types?
What you need to know
A data type tells Python what a value is and what you can do with it. You can add two ints, but you cannot add an int to a str.
| Family | Types | Example | Mutable? | Typical AI use |
|---|---|---|---|---|
| Numbers | int, float, complex | 42, 0.87, 2+3j | No | counts, scores, probabilities |
| Boolean | bool | True | No | flags like is_fraud |
| Text | str | "refund please" | No | prompts, documents |
| Sequences | list, tuple, range | [1, 2], (1, 2) | list yes, others no | batches, records |
| Mapping | dict | {"role": "user"} | Yes | JSON, configs |
| Sets | set, frozenset | {"a", "b"} | set yes | unique IDs |
| Binary | bytes, bytearray | b"\x89PNG" | bytearray yes | images, audio |
| Null | NoneType | None | No | "no value yet" |
Checking a type
1x = True2print(type(x)) # <class 'bool'>3print(isinstance(x, int)) # True -> bool is a subclass of int4print(True + True) # 25print(type(None)) # <class 'NoneType'>type(x) gives the exact type. isinstance(x, SomeType) also accepts subclasses, so it is the right check in real code. The surprise above is worth remembering: bool is a subclass of int, so True behaves like 1 in arithmetic. That is handy for counting — sum(flags) counts the True values — and a trap when you validate input, because isinstance(True, int) passes.
What Python does not have
There is no separate char type: a single character is a str of length 1. There is no built-in fixed-size array like in C or Java; the list is the general container, and numeric arrays come from NumPy (ndarray) or PyTorch (Tensor), which are third-party types.
A real-life example
You call an LLM that classifies a customer message and returns JSON. The json module turns that text into built-in types:
1import json23raw = '{"label": "refund", "confidence": 0.93, "tags": ["upi", "failed"], "escalate": false, "agent_id": null, "attempts": 2}'4reply = json.loads(raw)56for key, value in reply.items():7 print(f"{key:<10} {type(value).__name__}")8# label str9# confidence float10# tags list11# escalate bool12# agent_id NoneType13# attempts intJSON true/false become bool, null becomes None, arrays become list and objects become dict. Knowing this mapping is how you write code that reads model output safely — for example, checking reply["agent_id"] is None before using it.
Follow-up questions to expect
- "What is the difference between
type()andisinstance()?" —type()returns the exact class;isinstance()returnsTruefor subclasses too, so it respects inheritance and is preferred for checks. - "Is
boola number?" — Yes,boolsubclassesint, soTrue == 1andTrue + True == 2. - "Why does Python have both
bytesandstr?" —stris text made of Unicode characters;bytesis raw 8-bit data. You convert with.encode("utf-8")and.decode("utf-8"), which matters when you read files or send data over a network.