Unlocking the Mystery of Python’s FOR Loop: A Teacher’s Perspective

A photo of a child holding a banner of Python code in front of balloons and a rocket

Posted by
John-Lee Langford

One of the most common hurdles students face when first learning Python is understanding the FOR loop. A common exercise is to print "Hello World" five times:

for i in range(5):
	print("Hello World")

It starts off very simply, but then the puzzled looks begin when we explain how a loop variable works, or how the range() function defines the start, stop, and step values of a sequence. In my experience, Python’s user-friendly nature sometimes obscures what’s actually happening under the hood, leaving students without a solid mental model of iteration.

I’ve found that introducing FOR loops through languages like JavaScript or C can make a world of difference. Here’s why:

The Problem with Python’s FOR Loop Simplicity

Python’s FOR loop is deceptively simple. Its syntax reads almost like English. To output "0 1 2 3 4", we can use the following code:

for i in range(5):
	print(i)

While this simplicity is great for seasoned developers, it can be too abstract for beginners. Common questions include:

"Why is the first number zero?"

"Why isn't the last number five?"

"What is 'i'?"

Here's why students struggle:

  1. The Loop Variable (i): They see i changing but can’t quite grasp that Python is assigning the value of the current iteration to it, or even that i is a variable.
  2. The range() function: The parameters of range(start, stop, step) aren’t intuitive for many. The fact that stop is exclusive adds another layer of confusion.
  3. Implicit counter initialisation: Python’s for i in range() abstracts away the counter’s initialisation and incrementation, which are crucial for understanding how loops work.

Without seeing the mechanics of a loop laid bare, students often struggle to internalise the concept. Of course, rather than just teaching the stop parameter, we could teach all three parameters at the same time:

for i in range(0, 5, 1):
	print(i)

It may give a greater level of understanding for some students, but just presenting students with additional numbers is likely to increase confusion for most.

Why JavaScript or C Makes FOR Loops Easier to Grasp

Languages like JavaScript and C force students to confront the details Python hides. For example, a basic FOR loop in JavaScript looks like this:

for (let i = 0; i < 5; i++) {
	console.log(i);

At first glance, this is much more complicated than Python's syntax, but the structure is more explicit and helps students understand key aspects of iteration:

  1. Initialisation: let i = 0 clearly shows we are declaring a variable and that the loop variable starts at a specific value.
  2. Condition: i < 5 makes it clear that the loop continues as long as the condition is true.
  3. Incrementation: i++ (or even using i = i + 1) visibly increments the loop variable after each iteration.

The explicit syntax mirrors the logical steps students need to visualise in their minds.

Bridging the Gap Back to Python

Once students understand the mechanics of a FOR loop in JavaScript or C, they’re better equipped to appreciate Python’s simplicity. Here’s how I approach this transition:

  1. Start with JavaScript or C: Walk through the loop step-by-step, emphasising initialisation, the condition, and incrementation. Use a flowchart or hand-trace an example to show how the variable changes with each iteration.
  2. Compare with Python: Revisit the same loop in Python and explain how range() handles the start, stop, and step values. Show that while Python’s syntax is more concise, the underlying process remains the same.
  3. Hands-on practice: Have students write FOR loops in both languages to solidify their understanding.
  4. Explore advanced uses: Once they’re comfortable, introduce more complex scenarios like nested loops or using range() with custom steps.

The Results

By taking this approach, I’ve found that students develop a much stronger grasp of iteration. They no longer see the loop variable as a magic number but as an essential part of the process. When they return to Python, they’re able to appreciate its simplicity while understanding the mechanics behind it.

Final Thoughts

As teachers, we’re often tempted to lean on Python’s beginner-friendly syntax to ease students into coding. However, sometimes a step back into a more explicit language can be the key to unlocking deeper understanding. By showing students the inner workings of a FOR loop in JavaScript or C, we equip them with the tools they need to master Python — and beyond.

Opinions expressed here are the author's own and may not be representative of organisations they represent or are employed by.