Python Programming for Beginners
Lesson 2 of 3 7 min +55 XP

Loops in Python

Repeat work with for and while.

What you'll learn

  • Loop with for and range()
  • Use a while loop
  • Avoid infinite loops
The exercise-rep counter

A coach says 'do a push-up for each number up to ten' — you repeat one action, counting as you go. A Python for-loop is that counter, and a while-loop is 'keep going until you're too tired,' repeating until a condition changes.

for and while

for i in range(3):
    print(i)   # 0, 1, 2
range(n) stops before n

range(3) gives 0, 1, 2 — exactly three values.

Knowledge Check

+15 XP / correct

1. How many values does range(5) produce?

2. An infinite while loop usually happens when…