Computer Science, asked by ashwin456ojha, 1 year ago

How to easily understand the concept of nested loop(for and while, both) in Python?
Best answer will be marked as Brainliest answer! Keep explain easy.

Answers

Answered by Anonymous
14

hey mate here is your answer:-

Nesting a Loop

As simple as it seems, nesting a loop means simply having a loop (let's call it outer loop) that has inside its commands another loop (let's call it inner loop). For example, the code appearing below shows two nested loops, an outer for loop over the values of i and an inner for loop over the values of j to multiply inside the inner loop all nine elements of a 3x3 matrix A by a factor f that changes according to the outer loop iteration.

A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

f = 1

print(A)

for i in range(0, 3):

f *= 10

for j in range(0, 3):

A[i][j] *= f

print(A)

The output of such program will be:

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

[[10, 20, 30], [400, 500, 600], [7000, 8000, 9000]]

Therefore, the outer loop executes 3 iterations (i equal to 0, 1, and 2), and at each iteration it's executed:

The multiplication of the factor f by 10

The execution of the inner loop that has 3 iterations (j equal to 0, 1, and 2), and at each iteration the element i,j of A is multiplied by f

Similar questions