CBSE Class 12 Computer Science Question 80 of 105

Python Revision Tour II — Question 7

Back to all questions
7
Question

Question 4(b)

Predict the output of the following code snippet ?

Numbers = [9, 18, 27, 36]
for Num in Numbers :
    for N in range(1, Num % 8) : 
        print(N, "#", end=" ") 
    print( )
Answer
Output

1 # 
1 # 2 # 
1 # 2 # 3 # 
Explanation
  1. Numbers is a list containing the numbers 9, 18, 27, and 36.
  2. The outer for loop iterates over each element in the list Numbers.
  3. The inner loop iterates over the range from 1 to the remainder of Num divided by 8. For example, if Num is 9, the range will be from 1 to 1 (9 % 8 = 1). If Num is 18, the range will be from 1 to 2 (18 % 8 = 2), and so on. Then it prints the value of N, followed by a "#", and ensures that the output is printed on the same line by setting end=" ".
  4. After both loops, it prints an empty line, effectively adding a newline character to the output.