Class - 12 CBSE Computer Science Important Output Questions 2025
Predict the output of the following code:
line = [4, 9, 12, 6, 20]
for I in line:
for j in range(1, I%5):
print(j, '#', end="")
print()
Python
Python List Manipulation
25 Likes
Answer
1 #2 #3 #
1 #2 #3 #
1 #
Working
The list
linecontains the values [4, 9, 12, 6, 20].The outer loop iterates over each element (
I) in thelinelist.First Iteration (I = 4):
I % 5 = 4 % 5 = 4
Inner Loop Range:range(1, 4)→ This generates the numbers 1, 2, 3.
Output:1 #2 #3 #Second Iteration (I = 9):
I % 5 = 9 % 5 = 4
Inner Loop Range:range(1, 4)→ This generates the numbers 1, 2, 3.
Output:1 #2 #3 #Third Iteration (I = 12):
I % 5 = 12 % 5 = 2
Inner Loop Range:range(1, 2)→ This generates the number 1.
Output:1 #Fourth Iteration (I = 6):
I % 5 = 6 % 5 = 1
Inner Loop Range:range(1, 1)→ This generates no numbers (empty range).
Print: No output for this iteration (just a new line is printed).Fifth Iteration (I = 20):
I % 5 = 20 % 5 = 0
Inner Loop Range:range(1, 0)→ This generates no numbers (empty range).
Print: No output for this iteration (just a new line is printed).
The final output of the code will be:
1 #2 #3 #
1 #2 #3 #
1 #
Answered By
10 Likes