Iterating with for and range
Walk over numbers and sequences.
Iterating with for and range is a free Mojo Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Mojo Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Walk Over Items
When you want to visit each value in turn, reach for a for loop. It pulls one item at a time and runs the body for each. 🚶
The for Shape
Write for, a loop variable, the word in, something to walk over, then a colon and an indented block.
for item in things:
print(item)Count with range
To loop a fixed number of times, pair for with range. range(n) yields 0 up to but not including n.
for i in range(3):
print(i)range Stops Before the End
This catches beginners: range(5) gives 0, 1, 2, 3, 4. The end value is excluded, so you get exactly five numbers.
Choose a Start
Give range two values to set a start and end. range(2, 5) walks 2, 3, 4, beginning where you tell it.
for i in range(2, 5):
print(i)Add a Step
A third value sets the step between numbers. range(0, 10, 2) yields the even numbers 0, 2, 4, 6, 8.
for i in range(0, 10, 2):
print(i)Count Downward
A negative step lets range go backward. range(3, 0, -1) produces 3, 2, 1, perfect for a simple countdown.
for i in range(3, 0, -1):
print(i)Loop Over a List
for works on collections too. Iterating a List hands you each element directly, so you rarely need an index.
for name in names:
print(name)Index When You Need It
If you do need positions, loop over range(len(items)) and use the index to reach into the collection.
for i in range(len(items)):
print(items[i])Accumulate Across a for
Just like while, a for loop can build a total. Initialize before the loop, then add inside the body each pass.
var total = 0
for i in range(1, 5):
total += iPrefer for for Known Counts
Use for with range when the number of passes is known up front. It is clearer and harder to break than a manual while.
Quick Check
You write for i in range(5). Which exact sequence of numbers does i take across the loop?
Recap
A for loop visits each item, and range generates numbers with start, end, and step. Remember the end value is excluded. 🎯
Frequently asked questions
Is the “Iterating with for and range” lesson free?
Yes — the full text of “Iterating with for and range” is free to read here on the web, and the Mojo Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Mojo Academy course, upgrade to CoddyKit PRO.
What will I learn in “Iterating with for and range”?
Walk over numbers and sequences. You practise Mojo Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Mojo Academy?
No prior experience is required. Mojo Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Iterating with for and range” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Mojo Academy lesson?
Yes. Every Mojo Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Making Choices with if/else
- Looping with while
- Iterating with for and range
- break, continue, and Early Exit