Bird
0
0

You want to print all odd numbers from 1 to 15 using range(). Which code does this correctly?

hard📝 Application Q15 of 15
Python - For Loop
You want to print all odd numbers from 1 to 15 using range(). Which code does this correctly?
Afor i in range(1, 16, 2): print(i)
Bfor i in range(0, 15, 2): print(i)
Cfor i in range(1, 15): if i % 2 == 1: print(i)
Dfor i in range(1, 16): print(i % 2)
Step-by-Step Solution
Solution:
  1. Step 1: Identify odd numbers range

    Odd numbers from 1 to 15 are 1,3,5,...,15.
  2. Step 2: Check each option

    for i in range(1, 16, 2): print(i) uses range(1,16,2) which generates 1,3,5,...,15 directly. for i in range(0, 15, 2): print(i) starts at 0 (even). for i in range(1, 15): if i % 2 == 1: print(i) misses 15 (range(1,15) goes to 14) and uses extra if check. for i in range(1, 16): print(i % 2) prints 1 or 0, not numbers.
  3. Final Answer:

    for i in range(1, 16, 2): print(i) -> Option A
  4. Quick Check:

    range(1,16,2) = odd numbers [OK]
Quick Trick: Use step=2 starting at 1 for odd numbers [OK]
Common Mistakes:
MISTAKES
  • Starting at 0 for odd numbers
  • Forgetting to include last number
  • Printing modulo instead of number

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes