Loops
LockedLesson 6 of 60 · Syntax · Easy
Finish these first:Conditionals →Or pass the tests below to unlock it now.
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.
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.
Quick check
Which numbers does `range(1, 6)` step through in Python?
Your challenge
Use a for loop with range(1, 6) to add the numbers 1 through 5 into total (so total becomes 15).
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.
Learn this first
- ConditionalsNot yet