Post

Python Programming
Python Programming@PythonPr·
Python Question / Quiz; What is the output of the following Python code, and why? Comment your answers below!
Python Programming tweet media
English
47
9
162
15.9K
Coding Computing Coach
Coding Computing Coach@CodingComputing·
@PythonPr Answer: A (yes, the list `a` gets modified!) Solution: Let's see what's happening under the hood. a = [2,4,6,8,9] creates a list with those values, and attaches the name `a` to it. b = a this looks up the list referenced by `a`, and gives it an additional name, `b`. So now +
English
1
0
2
966
Coding Computing Coach
Coding Computing Coach@CodingComputing·
@PythonPr there is a list with those values, and there are two names attached to it: a and b. In other words, `a` and `b` are different names, but they refer to exactly the same object in memory. It's like having two names for the same dog. Now b[4] = 10 Here +
English
1
0
0
112
Coding Computing Coach
Coding Computing Coach@CodingComputing·
@PythonPr we are modifying the list referenced by `b`. But recall that that's the same list referenced by `a`. For this list, the value at index4 is changed to 10. Thus, the underlying list (referenced by both a & b) is now [2, 4, 6, 8, 10] Notice that, in this process +
English
1
0
0
117
Bagikan