Python - For LoopWhich 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)Check Answer
Step-by-Step SolutionSolution:Step 1: Check indentation rulesPython requires consistent indentation to define nested blocks. Inner loop must be indented inside outer loop.Step 2: Identify correct indentationfor 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.Final Answer:for i in range(3):\n for j in range(2):\n print(i, j) -> Option BQuick 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:MISTAKESMissing indentation for inner loopIncorrect indentation for printWriting loops on same line without colon
Master "For Loop" in Python9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More Python Quizzes Input and Output - Formatting using format() method - Quiz 7medium Input and Output - Formatting using format() method - Quiz 1easy Loop Control - Why loop control is required - Quiz 13medium Python Basics and Execution Environment - Python Block Structure and Indentation - Quiz 12easy Python Basics and Execution Environment - Comments in Python - Quiz 2easy Variables and Dynamic Typing - Type checking using type() - Quiz 7medium Variables and Dynamic Typing - How variable type changes at runtime - Quiz 3easy Variables and Dynamic Typing - Dynamic typing in Python - Quiz 1easy While Loop - Nested while loops - Quiz 8hard While Loop - Why while loop is needed - Quiz 2easy