Python: Why Changing "y" Can Sometimes Change "x" Too
You write:
x = 10
y = x
Then you change "y".
Sometimes "x" stays the same.
Sometimes "x" changes too.
That sounds strange until you understand what "=" actually does in Python.
First, look at this example
x = 10
y = x
x = 20
print(x)
print(y)
Output:
20
10
Changing "x" did not change "y".
Now look at this:
x = [10, 20, 30]
y = x
y.append(40)
print(x)
print(y)
Output:
[10, 20, 30, 40]
[10, 20, 30, 40]
This time, changing "y" also changed what we see through "x".
Why?
The answer is mutation and reassignment.
"y = x" does not create a copy
When you write:
x = [10, 20, 30]
y = x
Python does not create another list containing the same values.
Both names refer to the same list object.
So when you do:
y.append(40)
you are changing that list.
Since "x" also refers to that same list, "x" now shows:
[10, 20, 30, 40]
A simple way to remember it:
Reassignment → changes what a name refers to.
Mutation → changes the object itself.
Reassignment vs mutation
Consider this:
x = 10
y = x
x = 20
Here, "x" is reassigned.
It now refers to "20".
"y" still refers to "10".
But with a list:
x = [10, 20]
y = x
y.append(30)
"y.append(30)" changes the existing list.
So both "x" and "y" show:
[10, 20, 30]
This difference is one of those small Python concepts that becomes very important when working with real programs.
Mutable and immutable objects
This is where another Python concept becomes useful.
Some objects can be changed after they are created.
These are called mutable objects.
Common examples:
- "list"
- "dict"
- "set"
Some objects cannot be changed in place.
These are called immutable objects.
Common examples:
- "int"
- "float"
- "str"
- "tuple"
For example:
numbers = [10, 20, 30]
numbers.append(40)
The existing list is modified.
But:
x = 10
x = 20
does not modify the integer "10".
The name "x" is simply reassigned.
Here is another example that can confuse you
Predict the output before running this:
numbers = [10, 20, 30]
other_numbers = numbers
numbers = [100, 200, 300]
other_numbers.append(40)
print(numbers)
print(other_numbers)
The output is:
[100, 200, 300]
[10, 20, 30, 40]
Why?
Initially:
numbers = [10, 20, 30]
other_numbers = numbers
Both names refer to the first list.
Then:
numbers = [100, 200, 300]
This does not change the old list.
It makes "numbers" refer to a new list.
"other_numbers" is still referring to the original list.
So:
other_numbers.append(40)
changes only the original list.
One more Python variable trap: "input()"
There is another small thing that often causes confusion.
Look at this:
age = input("Enter your age: ")
print(type(age))
If you enter:
25
the result is:
Even though you entered a number.
"input()" returns a string.
If you actually need an integer:
age = int(input("Enter your age: "))
Now:
print(type(age))
gives:
This matters when you start doing calculations.
For example:
age = input("Enter your age: ")
print(age + 5)
This will not work as expected because "age" is a string.
Instead:
age = int(input("Enter your age: "))
print(age + 5)
Don't confuse "=" with "=="
Another basic mistake is confusing these two operators.
"=" means assignment:
age = 25
"==" means comparison:
age == 25
For example:
age = 25
if age == 25:
print("Age is 25")
A simple rule:
"=" → assign
"==" → compare
Python is dynamically typed
Python variables do not need a type declaration like some other programming languages.
You can write:
age = 25
and later:
age = "25"
The same name can refer to objects of different types at different times.
You can check the current type with:
print(type(age))
This is called dynamic typing.
Choose variable names that explain the value
Technically, this works:
a = 45000
b = 12
c = a / b
But this is easier to understand:
annual_salary = 45000
months = 12
monthly_salary = annual_salary / months
Good variable names make code easier to read, debug, and maintain.
Especially when you come back to your code after a few weeks.
Try these before you run them
Don't run these immediately.
Predict the output first.
Question 1
x = [1, 2, 3]
y = x
y.append(4)
print(x)
What will it print?
Question 2
x = 10
y = x
y = 20
print(x)
print(y)
What will it print?
Question 3
x = [1, 2, 3]
y = x
x = [10, 20, 30]
y.append(4)
print(x)
print(y)
Can you predict both outputs?
If you can explain why the answers are different, you understand the important part.
Want the Python Variable Basics in One Place?
If you're preparing for Python interviews or want a reference for Python syntax, variable naming rules, data types, dynamic typing, multiple assignment, scope, "input()", and related questions, I've collected those topics here:
Python Syntax and Variables Interview Q&A
Use it as a reference when you want to revise the broader Python syntax and variable concepts.
Final challenge
What will this program print?
items = ["SQL", "Python"]
copy_of_items = items
items = ["Python", "GCP"]
copy_of_items.append("BigQuery")
print(items)
print(copy_of_items)
Will ""BigQuery"" appear in both lists?
Don't run it immediately.
Think about these three things first:
- What object does "items" refer to initially?
- What happens when "items" is reassigned?
- Which object does "copy_of_items" still refer to?
That's the part of Python variables that is easy to miss when you're just learning the syntax.
Top comments (0)