Bird
0
0

Which of the following is the correct syntax for a nested for loop in Python?

easy📝 Syntax Q12 of 15
Python - For Loop
Which of the following is the correct syntax for a nested for loop in Python?
Afor i in range(3): for j in range(2): print(i, j)
Bfor i in range(3): for j in range(2): print(i, j)
Cfor i in range(3): for j in range(2): print(i, j)
Dfor i in range(3): for j in range(2): print(i, j)
Step-by-Step Solution
Solution:
  1. Step 1: Check indentation rules

    Python requires consistent indentation to define nested blocks. Inner loop must be indented inside outer loop.
  2. Step 2: Identify correct indentation

    for i in range(3): for j in range(2): print(i, j) uses 4 spaces indentation for inner loop and print statement, which is correct.
  3. Final Answer:

    for i in range(3):\n for j in range(2):\n print(i, j) -> Option B
  4. Quick Check:

    Proper indentation = for i in range(3): for j in range(2): print(i, j) [OK]
Quick Trick: Indent inner loop inside outer loop with spaces [OK]
Common Mistakes:
MISTAKES
  • Missing indentation for inner loop
  • Incorrect indentation for print
  • Writing loops on same line without colon

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes