Variables & Types
Python is dynamically typed but the types still matter. Learn what a variable really is, the core built-in types, and how to inspect them.
What you'll learn
- ✓ The difference between a variable and the value it points to
- ✓ The 6 built-in types you'll use 95% of the time
- ✓ How to inspect a value's type with type() and isinstance()
A variable in Python isn’t really a “box” that holds a value — it’s a name that points to an object somewhere in memory. This is more than a trivia; it explains some Python behaviors that surprise people coming from other languages.
A name → an object
(click Run)x is y is True because both names point to the same object. The
= did not copy the list; it just bound another name to it. To get an
independent copy, you’d write y = x.copy() or y = list(x).
The core built-in types
These six show up everywhere:
| Type | Example | Mutable? |
|---|---|---|
int | 42, -1000 | — |
float | 3.14, 1e-9 | — |
str | "hello" | no |
bool | True, False | — |
list | [1, 2, 3] | yes |
dict | {"a": 1} | yes |
You’ll also meet tuple ((1, 2), immutable list), set ({1, 2},
unordered, unique elements), and None (the absence-of-value singleton).
(click Run)type(x).__name__ gives the type name as a string. !r in the f-string
calls repr(), which gives an unambiguous representation — useful when
inspecting values.
Dynamic typing, but typed values
Python doesn’t require you to declare types — a variable can point at any type at any time. But every value has a type, and operations only work on compatible types.
(click Run)In production code you’ll add type hints to make the intent explicit
and let mypy or pyright catch type errors before runtime. We cover that
in a later lesson.
Casting between types
(click Run)isinstance vs type ==
Prefer isinstance(x, T) over type(x) == T — it respects inheritance,
which matters once you start using classes.
isinstance(True, int) # True — bool is a subclass of int
type(True) == int # False
Quick check
✦ Quick check
0/3 answeredFinished the lesson?
Mark it complete to track your progress and keep your streak alive. +20 XP