For loops & range · About 8 minutes

Again! Again! Again!

The big little idea

A loop repeats instructions. A for loop takes each item in a sequence, one at a time. That means you can do a big job with a small amount of code.

for number in range(1, 4):
    print(f"Jump {number}!")
print("Ta-da!")

range(1, 4) gives 1, 2, and 3. It stops before 4. Indent every line you want to repeat. A line that is no longer indented runs after the loop finishes.

Predict, then try: read the example from top to bottom. What do you think it will print? Copy it into the editor below to test your prediction.

Your turn, code explorer.

Print Star 1, Star 2, Star 3, Star 4, and Star 5, each on its own line.

Star 1
Star 2
Star 3
Star 4
Star 5
my_adventure.pyPython · in your browser

Edit the code. Then run it. Ctrl / ⌘ + Enter also works.

OutputYOUR IDEAS, ALIVE
Your program’s output will appear here.

Ready when you are. ✦

The first run loads Python. This can take a moment.

Program input For programs that use input()

Add answers before you run. Each input() reads the next line. Use made-up names and details.

Real Python, powered by Pyodide. Standard-library programs work here; desktop windows, turtle graphics, and package installation are not supported by this editor. Programs stop after 10 seconds of execution.

Back to the starting code?

This replaces your edits for this activity. Download your code first if you want to keep it.

A little hint, please

To include 5, the stop number in range must be 6.

Show one possible solution

There can be more than one way. Read this version, then try writing it yourself.

for number in range(1, 6):
    print(f"Star {number}")
Pack this idea for later: Loops reduce repetition. The end of range is not included.