Lesson 08 of 55
10 mins readPython For Loops & Multi-Dimensional Iteration
In Plain English
Explore for loops in Julia: standard range iteration (for i in 1:5), compact nested loops (for i in 1:2, j in 1:3), and parallel multi-collection iteration with zip.
Deep Dive: How It Works
Range Iteration: 1:5 iterates from 1 to 5 inclusive. 1:2:9 iterates with a step size of 2 (1, 3, 5, 7, 9).
Nested Loop Shorthand: for i in 1:2, j in 1:3 combines two nested loops into a single concise construct.
Enumerate and Zip: enumerate(coll) provides 1-based (index, value) pairs; zip(a, b) iterates through pairs from both collections simultaneously.
Core Rules to Remember

Compact Nested Loops: Write for i in 1:3, j in 1:3 instead of nesting two separate for blocks.

Step Ranges: Define start:step:stop ranges easily (e.g. 10:-1:1 for countdowns).
Live Interactive Example
Hit Run Code to see it liveRanges, Zip, and Multi-Index Loops
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Output Console
Click "Run Code" to view the rendered output.
How it works: 1:5 is inclusive. Multi-loop iterates col fastest (inner) and row slowest (outer).
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Calculate Sum of Evens
Initialize `total = 0`.
Loop through the even numbers from 2 to 10 with `for i in 2:2:10` and add `i` to `total`.
Print `"Total: "` followed by `total`.
1
2
3
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.