Python variables and types: give your shit a name
Updated · every example run on Python 3.9 to 3.15
In lesson 1 every value you printed was typed straight into the print. That works right up until you need the same number twice, or a number that changes, or a number you will only know later. A variable fixes that: it is a name you stick on a value so you can say the name instead of the value.
Think of it as a sticky note, not a box. The value lives somewhere in memory; the name is a label pointing at it. You can peel the label off and stick it on something else whenever you like, and Python will not say a word. That freedom is great, and it is also how most of this lesson's bugs happen.
Make a variable
A variable is born the first time you assign to it. Name on the left, one =, value on the right:
cat_name = "Mittens"
cat_weight = 4.5
print(cat_name, "weighs", cat_weight, "kg")Mittens weighs 4.5 kgThere is no keyword to declare it first and no type to announce. The = is not the "equals" from math class. It is an instruction: work out the thing on the right, then stick the name on the left onto it. So it only goes one way. Write 4.5 = cat_weight and Python refuses with a SyntaxError, because you can't stick a label on the number 4.5 itself.
Once the name exists, print(cat_name) and print("Mittens") do exactly the same thing. That is the whole trick.
The five types you meet first
Every value has a type, and the type decides what you can do with it. You don't write the type anywhere: Python reads it off the value. The built-in function type() tells you what it decided:
lives = 9
cat_weight = 4.5
cat_name = "Mittens"
is_hungry = True
vet_visit = None
lives_as_text = "9"
print(type(lives))
print(type(cat_weight))
print(type(cat_name))
print(type(is_hungry))
print(type(vet_visit))
print(type(lives_as_text))<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
<class 'NoneType'>
<class 'str'>The word "class" in there just means "type" for now; lesson 13 explains why Python says it that way. Here are the five:
- int is a whole number:
9,0,-40. - float is a number with a decimal point:
4.5, and also4.0. The dot is what makes it a float. - str is text, in quotes.
"9"is a string with one character in it, not a number, which is why the last line saysstr. Quotes win. - bool is
TrueorFalse, and nothing else. Is the cat hungry? The cat is always hungry. - None is Python's way of saying "nothing here yet". There is no vet visit booked, and
Nonesays so on purpose. It is the only value of a type calledNoneType, which is the nametype()printed.
True, False and None start with a capital letter, and they have to. Type true and Python has no idea what you mean: that is a NameError, the same one you will meet at the end of this lesson.
Reassigning: the label moves
Assign to a name that already exists and the label moves to the new value. The old value is simply forgotten. Watch a bank balance have a bad week:
balance = 100
print("Payday:", balance)
balance = balance - 30
print("After groceries:", balance)
balance = balance - 85
print("After the concert tickets:", balance)
balance = "overdrawn"
print("Status:", balance)Payday: 100
After groceries: 70
After the concert tickets: -15
Status: overdrawnbalance = balance - 30 looks like nonsense in math. In Python it reads fine, because the right side runs first: take what balance points at now, subtract 30, then move the label onto the answer.
Look at the last assignment, too. balance was an int and now it is a str, and Python did not complain at all. A name has no type; only the value it points at does. That is called dynamic typing, and it means nobody stops you from putting text where your program expects a number. You will feel that one in lesson 3.
What you can call a variable
The rules for names are short:
- Letters, digits and underscores only. No spaces, no hyphens:
cat-weightreads as "cat minus weight". - It can't start with a digit.
2nd_catis a SyntaxError;second_catis fine. - It can't be one of Python's keywords, like
if,for,class,TrueorNone. Those words are taken. - Case matters. These are three different variables:
cat = "Mittens"
Cat = "the neighbor's cat"
CAT = "the one screaming outside at 3 a.m."
print(cat)
print(Cat)
print(CAT)Mittens
the neighbor's cat
the one screaming outside at 3 a.m.Legal is not the same as good. Python's style guide, PEP 8, says variable names are lowercase words joined by underscores, like cat_weight and days_until_payday. Pick names that say what the thing is. You will reread this code at 2 a.m. some day, and x2 will tell you absolutely fucking nothing.
Don't name things print
Here is a trap the rules above don't catch. print is not a keyword; it is just a name, like cat_name, that Python set up for you before your program started. And you already know what happens when you assign to a name that exists:
print = "Remember to feed the cat"
print("Hello")Traceback (most recent call last):
File "/home/me/project/main.py", line 2, in <module>
print("Hello")
~~~~~^^^^^^^^^
TypeError: 'str' object is not callable| Python | Prints |
|---|---|
| 3.9–3.15 | TypeError: 'str' object is not callable |
Run on CPython 3.9.6, 3.10.22, 3.11.17, 3.12.15, 3.13.17, 3.14.9 and 3.15.1, on November 11, 2026.
The first line moved the label: print now points at a string. The second line tries to call that string like a function, and a string can't be called, so you get TypeError: 'str' object is not callable. Python 3.13 and later also underline the call that failed. The full story is on the page TypeError: 'str' object is not callable.
The same goes for type, str, int, list, sum and the other built-in names. Python lets you reuse every one of them, and the moment you need the original back, all hell breaks loose. Call the variable reminder and move on.
NameError: the name that never existed
Now the error this lesson promised. Use a name that was never assigned, and Python stops dead. Usually the cause is a typo:
cat_weight = 4.5
print("Mittens weighs", cat_wieght)Traceback (most recent call last):
File "/home/me/project/main.py", line 2, in <module>
print("Mittens weighs", cat_wieght)
^^^^^^^^^^
NameError: name 'cat_wieght' is not defined. Did you mean: 'cat_weight'?| Python | Prints |
|---|---|
| 3.9 | NameError: name 'cat_wieght' is not defined |
| 3.10–3.15 | NameError: name 'cat_wieght' is not defined. Did you mean: 'cat_weight'? |
Run on CPython 3.9.6, 3.10.22, 3.11.17, 3.12.15, 3.13.17, 3.14.9 and 3.15.1, on November 11, 2026.
Read the last line first: it names the error and the name Python couldn't find. Python 3.9 just says the name is not defined. From 3.10 on it also asks "Did you mean: 'cat_weight'?", because it noticed a name that is spelled almost the same, and from 3.11 on it underlines the bad name with ^^^^.
The other usual causes: wrong capitals (Cat_weight), the lowercase true from earlier, or using a name on a line above the one that assigns it. Python runs the file top to bottom and doesn't read ahead. More on all of them in NameError: name is not defined.
Try it yourself
Make four variables for a coffee order: the drink's name (a str), its price (a float), how many shots it has (an int) and whether it comes with oat milk (a bool). Print all four on one line, then print the type of the price.
Show the answer
drink = "flat white"
price = 4.75
shots = 2
oat_milk = True
print(drink, price, shots, oat_milk)
print(type(price))
Your names can be different. If they are lowercase words joined by underscores, PEP 8 approves.
Questions
How do I declare a variable in Python?
You don't. There is no var or let and no type to write: assigning a value with = creates the variable on the spot.
How do I check the type of a variable in Python?
Pass it to type(), as in print(type(price)). Strictly, you are asking about the value the name points at, because a Python name has no type of its own.
Can a Python variable change its type?
Yes. Assign a string to a name that held a number and Python accepts it without a warning. You can write type hints such as price: float = 4.75, but Python itself does not check them when the program runs.
What does None mean in Python?
None is the value that means "no value". It is the only value of its type, NoneType, and it is written with a capital N.
Sources
- An informal introduction to Python, Python documentation
- Built-in functions: type, Python documentation
- Built-in types, Python documentation
- Lexical analysis: names and keywords, Python documentation
- PEP 8: function and variable names, Python Enhancement Proposals
Read on November 11, 2026.