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.
can you explain b part
ReplyDeleteFor i = 1
Deletefor 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.
Program to swap the first half elements with the second half elements of a list of numbers.
ReplyDeletePlease click on link to get answer https://www.pathwalla.com/2022/05/program-to-swap-first-half-elements.html
DeletePost a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )