Variables and names
Understand that a Python variable is a name bound to an object, and use assignment, swapping and constants correctly.
What you will learn
- Explain assignment as binding a name to an object
- Use multiple assignment and swap two names
- Remove a name with del, and avoid shadowing built-in names
On this page
In many languages a variable is a labelled box that holds a value. In Python it is closer to a name tag that you stick on an object. That difference explains several behaviours that otherwise look odd.
The fundamentals lesson on variables covers assignment and scope in general. Here we focus on how Python does it.
Assignment binds a name
name = value does not copy the value into a box. It makes name refer to the value, and a later assignment simply moves the tag.
count = 10count = count + 1print(count)11count = count + 1 first works out the right side (11), then binds the name count to that new value. The old 10 is simply no longer tagged.
You can bind several names in one statement:
x = y = 0width, height = 4, 5print(x, y, width, height)0 0 4 5x = y = 0 binds two names to the same value. width, height = 4, 5 binds two names at once, one for each value on the right.
That second form gives Python its neat way to swap two names without a temporary variable. The right side is worked out first, then the names are bound:
width, height = 4, 5width, height = height, widthprint(width, height)5 4The pair of values on each side is called a tuple. Lesson 20 covers tuples properly, and for now it is enough to know that this one-line swap works.
Names and objects
Two names can refer to the same object. The is operator asks whether two names refer to the exact same object, which is a stricter question than whether they have equal values.
first = "spam"second = firstprint(first is second)
second = second + "!"print(first, second)print(first is second)Truespam spam!FalseAfter second = first the answer is True: one object, two names. The line second = second + "!" creates a new string and moves the second tag to it, so first keeps the original.
Strings and numbers cannot be changed after they are made, so sharing them is harmless. Lists can be changed, and there a shared name shows up as a surprise. Lesson 19 teaches lists. For now, read the square brackets in this preview as “a collection of three numbers”, and append as “add one to the end”:
a = [1, 2, 3]b = ab.append(4)print(a)print(a is b)[1, 2, 3, 4]Trueb = a does not copy the list. Both names point at the same one, so growing it through b changes what a sees. If you keep one rule from this lesson, keep this one: assignment never copies an object. The lists lesson shows how to make a real copy.
Choosing names
A name may contain letters, digits and underscores, cannot start with a digit, and is case-sensitive, so Total and total are different. Names must not be Python’s reserved words such as if, for or class.
Python has no constants. MAX_RETRIES = 3 is only a convention: capital letters tell readers not to reassign it, and nothing stops you if you do.
A few names, such as match, case, type and lazy Requires version 3.15 or later, are soft keywords. They have special meaning only in specific places, so you can still use them as ordinary variable names, though it is usually clearer not to.
Removing a name
del name removes a name, so using it afterwards is an error. You will rarely need it, but it helps to see what happens when a name hides something else, as in the next section.
Shadowing built-ins
The most damaging naming mistake is shadowing a built-in. Names like int, str, print and input are built-in functions. If you assign to one, your variable hides the function.
print(int("42"))int = 5print(int)del intprint(int("42"))42542After int = 5, calling int("42") would raise TypeError: 'int' object is not callable. Here del int removes our variable, and the built-in becomes visible again. In real code, simply pick a different name such as count.
Common pitfalls
- Using a name before it exists.
print(total)before anytotal = ...raisesNameError. - Typos create new names. Writing
conut = count + 1quietly binds a new name. Editors and linters catch many of these. - Believing
b = acopies. See the list example above. - Reusing a built-in’s name such as
list,maxorsumfor your own variable. Pick a more specific name.
Without running it, work out what the last line prints. Then run it and compare. Input: none. Expected output: one line.
a = 5b = aa = a + 1c = bb, c = c + 10, aa = "five"d = ad = d + "!"print(a, b, c, d)Show solution
five 15 6 five!Step by step: a and b are 5. Then a becomes 6, and c takes b’s value, 5. The pair assignment evaluates the right side first, giving (15, 6), so b is 15 and c is 6. Finally a is rebound to "five", d gets the same string, and d + "!" creates a new one.
Predict the output, then run it. Input: none. Expected output: one line with three words.
a = "left"b = "right"a, b = b, ac = aa = "centre"print(a, b, c)Show solution
centre left rightAfter the swap, a is "right" and b is "left". Then c is tagged onto the same "right" string, and a moves on to "centre". Moving a does not touch c.
This program is meant to print 5 43, but it crashes. Rename the variable so the program works.
int = 5print(int, int("42") + 1)TypeError: 'int' object is not callableShow solution
count = 5print(count, int("42") + 1)5 43The variable no longer hides the built-in int, so int("42") converts the text again.
Summary
- A Python variable is a name bound to an object. Assignment never copies.
a, b = b, aswaps two names.istests whether two names share one object, anddelremoves a name.- Constants are a naming convention. Avoid names that shadow built-ins.
That completes the first module. The next module covers numbers and text in depth.