+ 1

Please help, I’m struggling to understanding this code

nums = [[1, 2, 3]] initializer = 1 for i in range(1): initializer *= 10 for j in range(1): nums[i][j] *= initializer print(nums)

27th Sep 2021, 10:45 PM
Jenkins
1 Réponse
0
Both for loops have only a single iteration (with i=0 and j=0). That means that initializer is multiplied by 10 exactly once so initializer=10. Since i and j are both 0 nums[i][j] is nums[0][0]. So it refers to the first element of the first list in nums. That is 1. Since it gets multiplied by initializer it is overwritten with 10. So in the end nums is [[10, 2, 3]].
27th Sep 2021, 11:19 PM
Simon Sauter
Simon Sauter - avatar