[Solved] Beginner Python Objective Specific Explanation [closed]


when you say

A = [ [1, 1, 2], [2, 2, 3], [3, 3, 4] ]

A is a list of lists. So,

A[0] gives you [1,2,3]
A[1] gives you [2,2,3]

A[0][0] is nothing but the 0th index of [1,2,3] which is 1.

and similarly, A[1][1] is the 1st index is [2,2,3] which is 2(the 2nd 2)

Now going back the program,

range(2) returns a list from 0 to 2, not including 2, so thats

[0,1]

so your looping through your program twice with i value being 0 and 1.

For the first line in the loop,

x = x + A[i][i]

becomes

x = 0 + A[0][0]

which is

x = 0 + 1

That explains your first print.

When you enter the loop again, you get this :

x = 1 + A[1][1] (because i is 1 and x was set to 1 in the previous loop.

which evaluates to

x = 1 + 2

Now the value of x is 3 and that explains your 2nd print statement.

The print outside the loop prints the current value of x, which is 3 and
thats why you get

1,3,3

solved Beginner Python Objective Specific Explanation [closed]