Ad space (banner)
🐍Python Lessons
Lesson 4 / 69

Loops (for statements)

This lesson covers looping with Python's for statement and range(), so you can repeat the same operation efficiently. It's written for anyone searching "Python for loop range" or "Python loops for beginners".

Python's for loop is most often paired with range(). range(1, 6) produces the numbers 1 through 5, one at a time. Instead of writing the same line over and over, a loop lets you express a large number of repetitions in just a few lines. Python's for loop can also iterate directly over things like lists and strings, which often makes it simpler to write than for-loops in other languages.

The sample code uses range(1, 6) to pull out the numbers 1 through 5 one by one, printing a "loop number N" message each time. Note that range(start, end) does not include the end value — range(1, 6) stops at 5, not 6.

A common beginner mistake is misjudging that end value. If you intend "1 through 5" and write range(1, 5), you'll only get 1 through 4. This off-by-one error is extremely common, so get in the habit of checking the actual output as you go.

In real development, for loops show up everywhere — displaying a list of products one by one, retrying a network request a fixed number of times, and more. Because Python's for loop can iterate over lists directly, it's often more concise than the equivalent in other languages.

Python
OUTPUT

💡 The Python engine may take a few seconds to load the first time you run code.

Ad space (banner)
Ad space (in-article)