a=b=[0] print(a[0]) b[0]=1 print(a[0])
Why was the value of b automatically substituted for the value of a when the value of a was not adjusted except when the list was first declared in the code above? If I declare a list = a list, does it not end with one declaration, but treat the two lists as one? Please tell me why
python programming program
Rather than two lists being treated as one, two variables point to one object.
"In Python, objects are stored as references. Assigning a
to b
means that b
points to the object that a
points to, not a
. If you want to assign a value, you can copy it." (Source)
We've created fiddle, so test and study it.
© 2024 OneMinuteCode. All rights reserved.