If you assign one variable to another, have you made a copy? In Python, the answer depends on what you do next. Rebinding a name and changing a mutable object are different operations.
This short lab prints both behaviors side by side. It also shows why a list copy can still share nested data, why a numeric-looking string remains text, and how to avoid a shared default list.
Table of Contents
Run the whole experiment first
Use Python 3.10 or later. Save this as variables.py in a folder named python-variable-lab, then run python3 variables.py. Your Python command may be python or py. No package installation or input file is needed.
def add_label(labels=None):
if labels is None:
labels = []
labels.append('checked')
return labels
def run():
planned = 3
remaining = planned
planned = planned + 1
print(f'Counts: planned={planned}, remaining={remaining}')
tasks = ['read', 'run']
same_tasks = tasks
copied_tasks = tasks.copy()
same_tasks.append('change')
print('Original:', tasks)
print('Alias:', same_tasks)
print('Copy:', copied_tasks)
groups = [['read']]
shallow_copy = groups.copy()
shallow_copy[0].append('run')
print('Nested original:', groups)
raw_count = '3'
print('Text repetition:', raw_count * 2)
print('Numeric addition:', int(raw_count) + 2)
first = add_label()
second = add_label()
print('Fresh defaults:', first, second, first is second)
if __name__ == '__main__':
run()
The expected output is:
Counts: planned=4, remaining=3
Original: ['read', 'run', 'change']
Alias: ['read', 'run', 'change']
Copy: ['read', 'run']
Nested original: [['read', 'run']]
Text repetition: 33
Numeric addition: 5
Fresh defaults: ['checked'] ['checked'] False
A name refers to a value
At the start, planned and remaining both refer to the integer 3. The expression planned + 1 produces a new integer value, and the assignment makes planned refer to 4. It does not change the integer 3 into a different object. Remaining still refers to 3.
Choose names that describe their role: planned, remaining, and completed communicate more than x, y, and z in this example. A short local name can be fine when its purpose is clear. Avoid reusing names such as list, str, or sum for unrelated values because later calls to the built-in functions become confusing.
Changing a list is different from rebinding a name
The statement same_tasks = tasks gives the same list a second name. Calling append through either name changes that one list, so both printouts include change. There is no independent backup merely because the names differ.
Calling tasks.copy creates a new outer list. Because copied_tasks was made before the append, its two original string elements remain unchanged when the other outer list grows. For a flat list of immutable strings, that may be all the separation you need.
A shallow copy still shares nested objects
The groups example has a list inside a list. Copying the outer list does not recursively copy the inner list. Appending through shallow_copy[0] therefore changes the inner list visible through groups[0] too.
If nested data must be independent, decide what independence means for that structure. You might rebuild the required lists explicitly or use copy.deepcopy for suitable objects. A deep copy is not a general replacement for thinking about ownership, shared resources, and data size.
Input text needs deliberate conversion
The value ‘3’ contains a digit, but it is still a string. Multiplying it by 2 repeats its text and prints 33. Converting it with int and then adding 2 produces 5. Converting ‘three’ instead raises ValueError.
A real input form should catch that conversion failure and explain what the user can enter. Do not silently replace every invalid count with zero unless that is an explicit rule. Also remember that ordinary floating-point values are not a complete design for exact monetary calculations.
Make a fresh default list for each call
Python evaluates function defaults when the function is defined. A list used directly as a default would therefore be shared across calls that omit the argument. The lab uses None as a sentinel and allocates the list inside the function.
The last line prints False because first and second are different list objects, even though they contain equal text. Equality compares their contents in this case; is compares identity. Use equality for ordinary value comparisons and identity for cases such as checking None.
If you pass an existing list to add_label, the function intentionally mutates that supplied list. The safe default does not make the function non-mutating. A caller who needs an unchanged input can pass a copy or use a function designed to return a new list.
Try changing one line at a time
- Move copied_tasks = tasks.copy() below the append. Its initial contents now include change.
- Replace same_tasks.append(‘change’) with same_tasks = [‘change’]. Tasks now keeps its original two items because you rebound the alias instead of mutating the shared list.
- Give add_label a list of your own. Print that list after the call to see its new item.
- Replace raw_count with ‘three’ and observe the conversion error. Restore the sample afterward.
Predict the output before running each version. The useful habit is asking which object an operation changes, rather than memorizing a rule that every assignment either copies or shares everything.
Download all the example files
The ZIP contains this original CampCodes example, a README, and an MIT license. No account, package installation, or paid service is needed.
References and verification
The Python introduction covers assignment, strings, and lists; the function tutorial explains default arguments. This AI-assisted rewrite was checked on October 22, 2026 with Python 3.13.5. Automated checks compared every output line, verified separate default lists and intentional mutation of a supplied list, and confirmed the failed integer conversion.
