I'm a noob in Python. I saw this code and I don't quite understand it.
list_1 = ['History', 'Math', 'Physics', 'CompSci']
list_2 = list_1
print(list_1)
print(list_2)
list_1[0] = 'Art'
print(list_1)
print(list_2)
After the program is executed, I know the list_1
is ['Art', 'Math', 'Physics', 'CompSci']
eventually, but why is the list_2
also ['Art', 'Math', 'Physics', 'CompSci']. I mean, there's no list_2 = list_1
after the second print(list_1)
. Could someone help me out? Thanks!
Update
I tried some new code.
a = 40
b = a
print(a)
print(b)
a = 50
print(a)
print(b)
And the result is
40
40
50
40
So this means, that problem I mentioned before is only for list, right? If I just have a normal variable like a
and b
, when I change the value of a
, the value of b
won't be affected.