Every program you will ever write has to remember things. A name, a price, a count of how many times a loop has run. In Python, a variable is how you give such a thing a name so that you can use it again.
Variables look easy, and they are, but there are a few ideas that trip up beginners for months. Does Python need you to declare the type? What names are allowed? Why does one list change when you only edited another? And why does x, y, z = "One" not do what many tutorials say? Getting these right early saves a lot of confusion later.
This tutorial explains variables from the start. We cover how to create one, the types of values it can hold, the naming rules, multiple assignment, how to print variables, type conversion, and scope. We also show the exact error that each common mistake gives. Every example was run on Python 3.13, and the output you see is the real output.
A variable is a name that refers to a value stored in your computer’s memory. You create it the moment you give it a value, with the equals sign. Python has no separate declaration command, such as int x in other languages.
In some other languages, a variable is the name of a memory location. That is a misleading picture of Python. In Python, a variable is a name that points to an object. The value lives in the object, and the name is a label on it. This matters, and we show why in the section on names and objects below.
Here is a short example with its output:
Notice that you do not need a type, or a semicolon at the end. You can use single or double quotes for text, and both are the same.
A variable can point to any kind of value. The most common ones are whole numbers, decimals, text, True or False, and None, which means no value.
| Type | Example | Used for |
|---|---|---|
| int | 10 | Whole numbers |
| float | 2.5 | Decimal numbers |
| str | “Asha” | Text |
| bool | True | Yes or no answers |
| NoneType | None | A value that means nothing yet |
Python is a dynamically typed language, which means a name has no fixed type. The object it points to has one. You can point the same name to an object of another type later.
This is flexible, but it also means that Python will not warn you if you change a type by accident. In big programs, type hints help. We show them near the end.
Here is the correct set of rules.
Total, total and TOTAL are three different names.class, if or for.Here Name and name are two different variables, which is why both print.
A name that starts with a number:
A name with a space:
A name with a hyphen. Python reads the minus sign as maths:
A reserved word:
If you create Total and then ask for total, Python does not find it:
Python 3.13 has 35 reserved words, also called keywords. You can list them yourself:
You cannot use these as names. Python also has built in names that are not keywords, such as list, str, print and sum. Python lets you use them as variable names, but that hides the original, and it breaks later code. This is called shadowing:
After list = [1, 2, 3], the name list no longer means the list type, so list("ab") fails. Pick a different name, such as numbers or items.
The Python style guide, PEP 8, says to write variable names in lower case, with words joined by underscores, such as total_marks. Use names that say what the value is. A name such as total_marks is much clearer than tm, or x, when someone reads your code next month.
Python has no true constants. By habit, programmers write the names of values that should not change in capital letters, such as MAX_MARKS. Python does not stop you from changing them:
The capital letters are a message to other programmers, and not a rule that Python enforces.
When you write b = a, Python does not copy the value. It makes b point to the same object as a. For numbers and text, that is safe, because those objects cannot be changed. When you give b a new value, it just points somewhere else:
With a list, the object can be changed, and that is where beginners get surprised. Both names point to the same list, so a change through one shows in the other:
Use the copy method when you want a separate list. This is one of the most common sources of bugs for a beginner, and it follows straight from the idea that a variable is a name, and not a box.
You can give several names their values in one line. The number of names must match the number of values.
If they do not match, Python raises an error:
It is easy to think that x, y, z = "One" gives the same value to all three names. It does something else, and it works only by luck, because One has three letters:
Python took the text and split it into letters. With a text of a different length, it fails:
To give the same value to several names, use chained assignment:
A warning for lists. With x = y = z = [], all three names point to the same list, as we saw above. For lists, give each name its own.
A very Pythonic trick. You do not need a third variable.
You can pull the items out of a list or tuple into names in one line.
A star takes the items that are left over:
The print function shows a value on the screen. The plus sign joins text, and joining text and a number gives an error. Here is a working example with the output:
A common slip is an extra plus sign in front of a name, such as print("hello", +z). It gives this error:
A comma in print puts a space between the items, and the plus sign joins text with no space. That is why you get HelloPython, with no space, in the joined line above.
This is the error you get when you join text and a number with a plus sign:
There are three ways to fix it. The last is the best, and is available from Python 3.6:
An f string starts with the letter f, and puts the variable inside curly brackets. It is easier to read, and there is no need to convert the number.
By default print separates items with a space. The sep argument changes that.
Here variables hold a price and a quantity, and the answer is built from them.
Text that looks like a number is still text. Use int, float or str to convert it. This matters most for input from a user or from a file, which always arrives as text.
If the text cannot be turned into a number, Python raises an error:
Where you create a variable decides where you can use it. A variable made inside a function is local, and exists only while the function runs. A variable made outside is global. If a function makes a name that is the same as a global one, it creates a new local one, and leaves the global one alone:
To change a global variable from inside a function, you must say so with the global keyword:
A local variable cannot be used outside its function:
Use global sparingly. It is usually cleaner to pass values into a function and return the answer.
The del statement removes a name. After that, using it is an error. This is rarely needed, but it is good to know.
Python lets you write the expected type next to a name. This is called a type hint. Python does not enforce it when the program runs, but editors and tools such as mypy use it to warn you.
Notice that Python let us put the text thirty in a variable that was hinted as int. A hint is a note, and not a lock. The tool mypy was not run for this page, so test it in your own setup.
| Error | Usual cause | Fix |
|---|---|---|
| NameError: name is not defined | A spelling or case mistake, or using a name before creating it | Check the spelling and the order |
| SyntaxError: invalid decimal literal | A name that starts with a number | Start with a letter or an underscore |
| SyntaxError: invalid syntax | A space in a name, or a reserved word used as a name | Use an underscore, or a different word |
| SyntaxError: cannot assign to expression | A hyphen in a name | Use an underscore |
| TypeError: can only concatenate str to str | Joining text and a number with a plus sign | Use str(), a comma, or an f string |
| TypeError: bad operand type for unary plus | A stray plus sign before a text value | Remove the plus sign |
| ValueError: not enough values to unpack | Fewer values than names in a multiple assignment | Match the number of names and values |
| TypeError: object is not callable | A built in name such as list was used as a variable | Rename the variable |
| SyntaxError on a curly quote | Code copied from a web page or a Word file | Type straight quotes in your editor |
total_marks.list, str, sum or max.MAX_MARKS.copy, and not an equals sign.Here is the full script that was used to test every example on this page. It ran on Python 3.13. The blocks that show an error are written to fail on purpose, so run those one at a time, or wrap them in try and except.
A variable is only a name, and once that idea is clear, many Python puzzles stop being puzzles. Type the examples, break them on purpose, and read the error each time. Then move on to data types in detail, starting with numbers and strings, and then lists and dictionaries.