...

/

Repeat Yourself

Repeat Yourself

Learn how to use loops to repeat code.

What if we wanted to plant 10 trees, send 5 reminders, or greet each student in a list without copying the same line of code repeatedly?

Instead of repeating the same line over and over, Python lets us loop through actions with just a few lines!

for loops: Do it a set number of times

A for loop lets us repeat actions a specific number of times, saving time and keeping our code clean and powerful. It is perfect for when we know how many times we want something to run.

Press + to interact
Looping over a piece of code
Looping over a piece of code

Try this:

Press + to interact
for i in range(5):
print("Hello!")

In this example, Python repeats the line five times.

In for i in range(5):, the colon (:) tells Python, ...