Python lesson 5

Simple Repetition

Tell Python to repeat something. Without writing it out ten times!

For Loops with Range

A for loop with range() tells Python: "repeat this block N times". The variable i counts each step, starting from 0. You can print i to see the counter change!

for i in range(3):
print("Go!") → prints Go! three times

for i in range(3):
    print("Go!")

Where Does range Stop?

for i in range(3):
    print(i)

Count with Range

Click Run to see the loop count from 0. Notice it starts at 0, not 1!

Hint: Click Run! range(5) gives: 0, 1, 2, 3, 4. five numbers starting from 0.

for i in range(5):
    print(i)

Repeat a Message

Run this code to see "Coding is fun!" 5 times. Then change 5 to 3. what happens?

Hint: Change 5 to 3 and click Run again. The message appears fewer times!

for i in range(5):
    print("Coding is fun!")

Count from 1

Run this code to count from 1 to 5. range(1, 6) starts at 1 and stops before 6!

Hint: range(1, 6) gives: 1, 2, 3, 4, 5. Try changing the start or end number!

for i in range(1, 6):
    print(i)

What you can do on CodeIt

Run the example, change one part, and use the result to check your understanding before moving to the next lesson.