GCGrowCarrer

Loops

Locked

Lesson 6 of 60 · Syntax · Easy

ConditionalsNext: Lists

Finish these first:Conditionals →Or pass the tests below to unlock it now.

1

The big idea

A for loop repeats an action automatically so you do not have to copy and paste the same line over and over. Use range(start, stop) to count through numbers (it stops right before stop): Just like if, put a colon : at the end of the for line and indent the code inside the loop by 4 spaces.

Think of it like this: Think of a loop like counting coins into a jar one by one — instead of writing the same line five times, you tell Python to repeat it.
Key words (tap to learn):
2

Read the lesson

A for loop repeats an action automatically so you do not have to copy and paste the same line over and over.

Use range(start, stop) to count through numbers (it stops right before stop):

coins = 0
for n in range(1, 4):   # runs for 1, 2, 3
    coins = coins + n   # coins becomes 6

Just like if, put a colon : at the end of the for line and indent the code inside the loop by 4 spaces.

3

Quick check

Which numbers does `range(1, 6)` step through in Python?

4

Your challenge

2 sample tests, 1 hidden

Use a for loop with range(1, 6) to add the numbers 1 through 5 into total (so total becomes 15).

Stuck? Get a hint (free)
More learning tools(compare languages, visual guide, related lessons)

Compare with another language

Similar to java.loops (90% overlap). Python `for x in it` ~ Java for-each; no `i++` in Python.

Visual walkthrough

A diagram, a step-by-step animation, and short captions for Loops.

Mastery0%