How to Create a Variable in Python
A variable is a name for a value, so you can use the value later by its name instead of typing it again. In Python you create one by writing the name, an equals sign and the value. This guide creates two variables on Python 3.15.2, gives one of them a new value, and shows the three errors beginners meet most.
Syntax
<name> = <value>
| Part | What it means |
|---|---|
<name> | The variable's name. It can contain letters, digits and underscores, but can't start with a digit |
= | Makes <name> refer to <value>. It doesn't compare anything |
<value> | The value to keep, such as text in quotes or a number |
The Python tutorial puts it in one line: "The equal sign (=) is used to assign a value to a variable." There's no separate step to announce a variable first. The line that assigns it creates it.
The Example
A script keeps the city Lisbon in a variable called city and the number 3 in one called visitors. It prints both, then gives visitors a new value, 4, and prints it again.
Prerequisites
- uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with
uv init.
Step 1: Write the Script
city = "Lisbon"
visitors = 3
print(city)
print(visitors)
visitors = 4
print(visitors)

"Lisbon" is in quotes because it's text, which Python calls a string. 3 has no quotes because it's a number. print() shows a value in the terminal; here it gets each variable by its name.
Step 2: Run It
uv run python variables.py
Lisbon
3
4

The first two lines are the values the variables were created with. The third is 4, because visitors = 4 made the name refer to the new value. The language reference describes assignment statements as being "used to (re)bind names to values", and a second assignment to the same name is exactly that: the name now points at the new value, and the old one is gone from it. The command works unchanged in PowerShell.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Using a variable above the line that creates it: print(city) first, city = "Lisbon" after | NameError: name 'city' is not defined | Assign it first. Python runs a script from the top down, so the name doesn't exist yet when print asks for it |
Changing a capital letter: print(City) after city = "Lisbon" | NameError: name 'City' is not defined. Did you mean: 'city'? | Spell it exactly as you created it. The reference says of names: "Case is significant." |
Starting a name with a digit: 2nd_city = "Porto" | SyntaxError: invalid decimal literal, and nothing in the script runs | Start with a letter or an underscore, such as second_city. Digits "cannot appear as the first character" |
The tutorial warns about the first one too: if a variable is not "defined" (assigned a value), "trying to use it will give you an error".
FAQ
Can a variable hold text first and a number later?
Yes. The name doesn't have a type; the value does. After visitors = 4 and then visitors = "four", print(visitors) printed four. Python allows it, but a name that keeps one kind of value is easier to follow.
Which names are allowed?
Letters (A–Z, a–z), the underscore and digits, where a digit can't come first. The names section of the reference also allows letters from other alphabets, and it says names "have no upper length limit". Python's style guide writes variable names in lowercase, with underscores between words, such as first_city.
Conclusion
city = "Lisbon" and visitors = 3 created two variables, and print() showed Lisbon and 3. A second assignment, visitors = 4, gave visitors a new value. A name has to be assigned before it's used, with the same capital letters, and it can't start with a digit.
Additional Resources
- Using Python as a calculator — the tutorial's first variables.
- Assignment statements — the reference for
=. - Names (identifiers and keywords) — the rules for variable names.
- PEP 8 naming conventions — how Python code usually names variables.
