Home>

### python - about 2d list

Contents

When I run the following code, I get [[0, 1, 2, 3], [1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6]]. but,
Why does a + on the 5th line do this? Isn't it the "sum" of the numbers?
Also, when + is converted to *, [[0, 0, 0, 0], [0, 1, 2, 3], [0, 2, 4, 6], [0, 3, 6, 9]] I'm not sure why. The first [0, 0, 0, 0] is 0 * ○○, so I can understand it, but the ones after that are unknown.
Thank you.

Corresponding source code
``````list_add = []
for x in range (4):
list_inner = []
for y in range (4):
list_inner.append (x + y)
list_add.append (list_inner)
print (list_add)>quoted text``````
• Answer # 1

``````list_add = []
for x in range (4):
Processing of this scope is executed when # x is 0 to 3 (4 times in total)
list_inner = []
# That is, the list_inner list will be declared four times.
for y in range (4):
# 4 more times than above (16 times with 4x4), pass here
list_inner.append (x + y)
Explain when # x is 0
The value obtained by adding y to # x is stored in the list variable above.
The value from # 0 + 0 to 0 + 3 is stored in list_inner
list_add.append (list_inner)
# If x is 0 above, list_inner will be [0, 1, 2, 3]
# Store it in list_add
# Do the above four times, which is the main loop
# So the result is in the question``````