Bird
0
0

How can you write a Python program that prints numbers 1 to 5 using a loop?

hard📝 Application Q9 of 15
Python - Basics and Execution Environment
How can you write a Python program that prints numbers 1 to 5 using a loop?
Awhile i <= 5: print(i) i += 1
Bfor i in range(1, 6): print(i)
Cfor i in range(5): print(i+1)
Dfor i in 1 to 5: print(i)
Step-by-Step Solution
Solution:
  1. Step 1: Use range() with start and stop

    range(1, 6) generates numbers 1 to 5 inclusive of start, exclusive of stop.
  2. Step 2: Use for loop with indentation

    for loops iterate over range, printing each number properly indented.
  3. Final Answer:

    for i in range(1, 6): print(i) -> Option B
  4. Quick Check:

    range(1,6) loops 1 to 5 [OK]
Quick Trick: range(start, stop) excludes stop number [OK]
Common Mistakes:
MISTAKES
  • Forgetting indentation
  • Using invalid for syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes