Lesson 15 of 55
8 mins readPython Numeric for Loops (start, stop, step)
In Plain English
The numeric `for` loop iterates a control variable from `start` to `stop` by an optional `step` increment. The loop variable is automatically local to the loop body.
Deep Dive: How It Works
Syntax: `for var = start, stop [, step] do ... end`.
Default Step: If step is omitted, Lua increments by 1.
Negative Step: `for i = 10, 1, -2 do` steps backwards.
Automatic Local: The iterator `var` is implicitly declared local and cannot be modified directly.
Core Rules to Remember

3-Part Numeric for: `for i = 1, 10, 2 do` iterates 1, 3, 5, 7, 9.

Read-Only Iterator: Never mutate the loop index variable inside the loop body.
Live Interactive Example
Hit Run Code to see it liveForward and Reverse Numeric Loops
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
Output Console
Click "Run Code" to view the rendered output.
How it works: Numeric for executed with positive step 2 and negative step -1.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Sum Range with Numeric for
Declare `local total = 0`.
Use `for i = 1, 5 do total = total + i end`.
Print `"Sum: " .. total`.
1
2
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.