Bird
0
0

What will this code print?

medium📝 Predict Output Q5 of 15
Python - For Loop
What will this code print?
for x in range(1, 3):
    for y in range(2):
        print(x + y, end=' ')
A0 1 1 2
B1 1 2 2
C1 2 2 3
D2 3 3 4
Step-by-Step Solution
Solution:
  1. Step 1: Identify values of x and y

    x runs 1, 2; y runs 0, 1 for each x.
  2. Step 2: Calculate sums and print

    For x=1: print 1+0=1, 1+1=2; for x=2: print 2+0=2, 2+1=3.
  3. Final Answer:

    1 2 2 3 -> Option C
  4. Quick Check:

    Sum of x and y printed in nested loops [OK]
Quick Trick: Add outer and inner loop variables for output [OK]
Common Mistakes:
MISTAKES
  • Starting range at 0 instead of 1
  • Confusing end=' ' usage
  • Mixing x and y values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes