Python - For LoopYou want to print only even numbers from 0 to 10 using a for loop. Which code correctly does this?Afor i in range(1, 11): if i % 2 == 0: print(i)Bfor i in range(0, 10, 2): print(i)Cfor i in range(0, 11, 2): print(i)Dfor i in range(10): if i % 2 == 0: print(i)Check Answer
Step-by-Step SolutionSolution:Step 1: Understand range parametersrange(start, stop, step) generates numbers from start up to stop-1, stepping by step.Step 2: Check which options print even numbers 0 to 10for i in range(0, 11, 2): print(i) uses range(0,11,2) which prints 0,2,4,6,8,10 correctly.Final Answer:for i in range(0, 11, 2):\n print(i) -> Option CQuick Check:range with step=2 prints even numbers [OK]Quick Trick: Use step=2 in range to get even numbers [OK]Common Mistakes:MISTAKESOff-by-one in range endStarting range at 1Not using step parameter
Master "For Loop" in Python9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More Python Quizzes Conditional Statements - Logical operators in conditions - Quiz 4medium Data Types as Values - Numeric values (int and float behavior) - Quiz 14medium Data Types as Values - Boolean values (True and False) - Quiz 13medium Input and Output - print() parameters and formatting - Quiz 13medium Loop Control - Continue statement behavior - Quiz 11easy Loop Control - Continue statement behavior - Quiz 2easy Operators and Expression Evaluation - Assignment and augmented assignment - Quiz 6medium Operators and Expression Evaluation - Comparison operators - Quiz 9hard Python Basics and Execution Environment - Why Python is easy to learn - Quiz 15hard Variables and Dynamic Typing - Naming rules and conventions - Quiz 3easy