[Type B] Python Revision Tour-1 – Chapter 1

|
Table of Contents

4. How many times will the following for loop execute and what’s the output?

Question 4. (a):

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

The loop is infinite because the range range(-1, 7, -2) will never generate any values as -1 is greater than 7 and the step is negative, so the loop never executes. Thus, the output will be nothing.

Question 4. (b):

for i in range (1, 3, 1) :
    for j in range (i+1) :
        print('*')
Output
*
*
*
*
*
*
Python
"Spread the light within, illuminate the hearts around you. Sharing is not just an action, but a sacred journey of connecting souls."~ Anonymous

Leave a Reply

Your email address will not be published. Required fields are marked *