14.2k views
1 vote
How many times will the print statement execute?

for i in range(1, 3):
for j in range(8, 12, 2):
print('{:d}. {:d}'.format(i, j))
a. 4
b. 6
c. 9
d. 36

User TvCa
by
7.8k points

1 Answer

5 votes

Answer: The nested loop in the code iterates through the range range(1, 3) for i and range(8, 12, 2) for j, so the code will execute as follows:

  • When i=1 and j=8, print statement executes.
  • When i=1 and j=10, print statement executes.
  • When i=2 and j=8, print statement executes.
  • When i=2 and j=10, print statement executes.

Therefore, the print statement will execute 4 times in total.

So the correct answer is a. 4.

User Henno
by
9.1k points

No related questions found