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:
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:
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.
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:
The explicit syntax mirrors the logical steps students need to visualise in their minds.
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:
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.
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.