Python Essentials for AI Engineer

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.

FamilyTypesExampleMutable?Typical AI use
Numbersint, float, complex42, 0.87, 2+3jNocounts, scores, probabilities
BooleanboolTrueNoflags like is_fraud
Textstr"refund please"Noprompts, documents
Sequenceslist, tuple, range[1, 2], (1, 2)list yes, others nobatches, records
Mappingdict{"role": "user"}YesJSON, configs
Setsset, frozenset{"a", "b"}set yesunique IDs
Binarybytes, bytearrayb"\x89PNG"bytearray yesimages, audio
NullNoneTypeNoneNo"no value yet"

Checking a type

Python
x = Trueprint(type(x))              # <class 'bool'>print(isinstance(x, int))   # True  -> bool is a subclass of intprint(True + True)          # 2print(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:

Python
import jsonraw = '{"label": "refund", "confidence": 0.93, "tags": ["upi", "failed"], "escalate": false, "agent_id": null, "attempts": 2}'reply = json.loads(raw)for key, value in reply.items():    print(f"{key:<10} {type(value).__name__}")# label      str# confidence float# tags       list# escalate   bool# agent_id   NoneType# attempts   int

JSON 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() and isinstance()?" — type() returns the exact class; isinstance() returns True for subclasses too, so it respects inheritance and is preferred for checks.
  • "Is bool a number?" — Yes, bool subclasses int, so True == 1 and True + True == 2.
  • "Why does Python have both bytes and str?" — str is text made of Unicode characters; bytes is 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.