0% found this document useful (0 votes)
18 views2 pages

While and For Output-09032025

The document contains multiple code blocks demonstrating various looping constructs in Python, including while loops and for loops. Each block illustrates different scenarios such as breaking out of loops, continuing iterations, and nested loops. The output of each code block varies based on the conditions set within the loops.

Uploaded by

renu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views2 pages

While and For Output-09032025

The document contains multiple code blocks demonstrating various looping constructs in Python, including while loops and for loops. Each block illustrates different scenarios such as breaking out of loops, continuing iterations, and nested loops. The output of each code block varies based on the conditions set within the loops.

Uploaded by

renu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Practice:

Write the output for following code blocks.

i=1 (1) i = 10 (2) x=1 (3)


while i <= 5: while i > 5: while x < 3:
print(i) print(i, end=" ") print("Hello")
i += 1 i -= 2 x += 1

count = 0 (4) while True: (5) i=1 (6)


while count < 3: print("Looping") while i < 10:
print(count) if i % 3 == 0:
count += 1 break
else: print(i, end=" ")
print("Done") i += 1

i=1 (7) i=1 (8) i=1 (9)


while i < 5: while i < 6: while i <= 3:
print(i, end=" ") if i == 4: j=1
i += 1 i += 1 while j <= 2:
else: continue print(f"i={i}, j={j}")
print("Else executed") print(i, end=" ") j += 1
i += 1 i += 1

x=0 (10) i=0 (11) while True: (12)


while True: while i < 5: num = int(input("Enter num(0 to stop): "))
x += 1 print(i, end=" ") if num == 0:
if x == 5: i=i+2 break
break else: print("You entered:", num)
print(x, end=" ") print("\nLoop completed") print("Loop ended")

i=1 (13) i=0 (14) i=1 (15)


while i < 10: while i < 5: while True:
print(i, end=" ") i += 1 if i % 7 == 0:
if i % 5 == 0: if i == 3: break
break continue print(i, end=" ")
i += 2 print(i, end=" ") i += 2

x = 10 (16) i=1 (17) i=1 (18)


while x > 0: while i < 4: while i < 4:
x -= 3 j=1 print(i, end=" ")
print(x, end=" ") while j < 3: i += 1
print(f"({i}, {j})", end=" ") else:
j += 1 print("End")
i += 1

i=5 (19) for i in range(3): (20) for i in range(2, 7, 2): (21)


while i > 0: print(i, end=" ") print(i, end=" ")
print("*" * i)
i -= 1
for i in range(5): (22) for i in range(5): (23) for i in range(5, 0, -1): (24)
if i == 3: if i == 2: print(i, end=" ")
break continue
print(i, end=" ") print(i, end=" ")

for i in range(3): (25) for char in "Hi!": (26) for i in range(1, 10, 3): (27)
for j in range(2): print(char, end=" ") print(i, end=" ")
print(f"({i}, {j})", end=" ")

(28) for i in range(1, 6): (29) for i in range(5, 0, -1): (30)


for char in "PYTHON"[::2]: print("*" * i) print("#" * i)
print(char, end=" ")

s = "PYTHON" (31) for i in range(0, 5, -1): (32) for i in range(6): (33)


for i in range(len(s)): print(i, end=" ") if i % 2 == 0:
print(s[i].lower(), end=" ") continue
print(i, end=" ")

for i in range(10): (34) for i in range(3, 0, -1): (35) for i in range(1, 10): (36)
if i % 3 == 0: print("X" * i) if i % 2 == 1 and i % 3 == 0:
if i % 2 == 0: print(i, end=" ")
print(i, end=" ")

for i in range(1, 10, 3): (37) s = "HELLO" (38) for i in range(5): (39)
print(i, end=" ") for i in range(len(s)): if i == 3:
if i % 2 == 0: print(s[i % 2], end=" ") break
break if i == 1:
continue
print(i, end=" ")

for i in range(6): (40) for i in range(1, 10): (41) for i in range(3): (42)
if i == 2: if i % 2 == 0: for j in range(3):
continue continue if j == 1:
if i == 4: if i > 5: continue
break break if i == 2:
print(i, end=" ") print(i, end=" ") break
print(f"({i}, {j})", end=" ")

You might also like