Q. How many times will the following for loop execute and what's the output?


(i)
for i in range(-1, 7, -2) :
    for j in range (3) :
        print(1, j)

(ii)
for i in range (1, 3, 1) :
    for j in range (i+1) :
        print('*')


Answer =

(a)
There is no output because of invalid range command.

(b)
This program execute 5 times

Output -

*
*
*
*
*

>>>

Explanation :-

For i = 1
for j in range (2):
So , j loop will execute 2 times.

For i = 2
for j in range(3):
So, j loop execute 3 times.

So, total no. Execution is 5.

4 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

  1. Replies
    1. For i = 1
      for j in range (2):
      So , j loop will execute 2 times.

      For i = 2
      for j in range(3):
      So, j loop execute 3 times.

      So, total no. Execution is 5.

      Delete
  2. Program to swap the first half elements with the second half elements of a list of numbers.

    ReplyDelete

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post