A Variable Is a Name
A variable in Python is a name you pin to a value. You write the name, an equals sign, and the value:
Three variables, three values, three types. Python happily holds them alongside each other, and you didn't have to tell it anything about what kinds of values they were. The value goes after the =; the name goes before it.
Once a variable exists, you can use it anywhere you'd use the value itself:
The names make the second-to-last line self-describing. That's the real win with good variable names — code that reads like an explanation.
Assignment Is Not Math
One thing that catches people coming from math classes: = in Python is not "equals." It's "assign." It means "take the value after the =, and pin it to the name before it." Python evaluates the value first, then does the assignment.
So this is perfectly legal:
The line count = count + 1 isn't claiming that count equals count plus one (mathematically impossible). It's saying: compute count + 1, then assign the result back to count. Python evaluates the expression first — 0 + 1, yielding 1 — and only then updates the variable.
You'll write count = count + 1 often enough that Python has a shortcut: count += 1 does the same thing.
Reassignment Changes the Label, Not the Value
A variable can point at a different value any time you want. The old value doesn't get modified — the variable simply starts referring to something else:
Notice that last line. mood holds a string for two lines and then suddenly holds an integer. Python didn't complain. This is dynamic typing — variables don't have a fixed type, only values do. The flexibility is helpful when prototyping and painful when a variable shifts meaning inside a function without your noticing. The safe habit is to pick a name that fits what the value represents, and then keep the variable representing that one thing for its lifetime.
The Rules for Variable Names
Python accepts names that:
- Start with a letter or an underscore (
_). - Contain letters, digits, and underscores.
- Are not a reserved keyword (
if,for,class,return, and a few dozen others).
So these are valid:
And these are not:
2nd_user— can't start with a digit.user-name— hyphens aren't allowed (Python reads them as subtraction).class— reserved keyword.user name— no spaces in names.
Names are case-sensitive, as you already know: total, Total, and TOTAL are three different variables. Python won't spot the typo for you.
Naming Conventions
Beyond the hard rules, the Python community has conventions — not enforced by the interpreter, but universal across the ecosystem:
lower_snake_casefor regular variables and functions:retry_count,fetch_profile.UPPER_SNAKE_CASEfor constants:MAX_RETRIES = 5.PascalCasefor class names (we'll get to those later).- Leading underscore (
_something) is a hint that a name is meant to be private. Not enforced — just a convention. - Trailing underscore (
class_) lets you use a name that would otherwise clash with a keyword.
Stick with these and your code will look like the Python other people have written.
Names Should Describe Meaning
A variable name is free to type but expensive to read. Spending a couple of extra characters to pick a name that actually describes what the value means saves future readers (usually future you) a lot of squinting.
Compare:
Same code, same answer — but only the second version tells you what's going on. Short names (i, n, x) are fine in very small scopes like loop counters, but outside those, spell things out.
Multiple Assignment (Use Sparingly)
Python lets you assign the same value to several variables in one line:
Or unpack several values at once:
Both are fine when they make the code clearer (like unpacking the return value of a function). The first form — chained = = = — is worth a moment of care: all three variables end up referring to the same object, which matters for mutable types like lists (more on that later).
What You Take Away
name = valuecreates a variable.- Variables don't have types; values do.
- Names follow a few hard rules and a lot of soft conventions — both are worth respecting.
- A descriptive name is the cheapest form of documentation.
Next up: what kinds of values Python actually supports — integers, floats, strings, booleans, and why it helps to know which is which.
Frequently Asked Questions
How do you create a variable in Python?
Pick a name, put an = after it, and put a value on the right. age = 30 creates a variable named age with the value 30. Python figures out the type automatically — you don't declare it.
What are the rules for Python variable names?
A name can contain letters, digits, and underscores, but can't start with a digit. It can't be a Python keyword like if or class. Names are case-sensitive: total, Total, and TOTAL are three different variables. The community convention is lower_snake_case.
Can a Python variable change type?
Yes. Python is dynamically typed, so a variable that holds a number one moment can hold a string the next. That flexibility is convenient but can surprise you — if a variable's meaning changes halfway through a function, that's usually a sign to pick a new name.