Simple question! Please answer quickly (Error in Python list?

Asked 2 years ago, Updated 2 years ago, 119 views

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

2022-09-21 19:38

1 Answers

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.


2022-09-21 19:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.