Last modified: Mar 28, 2026 By Alexander Williams
Python For Loop Range: A Beginner's Guide
Python's for loop is a fundamental tool. It lets you repeat actions easily. When paired with the range() function, it becomes incredibly powerful. This combination is perfect for automating repetitive tasks.
This guide will explain how to use for loops with range(). We will cover the syntax, parameters, and practical examples. You will learn to control iteration like a pro.
Understanding the Python For Loop
A for loop iterates over a sequence. This sequence can be a list, string, or tuple. The loop runs a block of code for each item in the sequence.
Here is a simple example using a list.
# Iterating over a list of fruits
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
apple
banana
cherry
But what if you need to run a loop a specific number of times? Manually creating a list like [0, 1, 2, 3, 4] is inefficient. This is where the range() function shines.
Introducing the Range Function
The range() function generates a sequence of numbers. It is memory-efficient. It produces numbers one at a time instead of creating a full list in memory.
This makes it ideal for loops. You can learn more about its full capabilities in our detailed Python Range Function Guide: Syntax & Examples.
The range() function can be used in three ways. It can take one, two, or three arguments.
1. Range with One Argument: range(stop)
With one argument, range(stop) starts from 0. It goes up to, but does not include, the stop value. The sequence increases by 1 each time.
# Loop from 0 to 4
for i in range(5):
print(f"Iteration number: {i}")
Iteration number: 0
Iteration number: 1
Iteration number: 2
Iteration number: 3
Iteration number: 4
The loop runs exactly 5 times, with 'i' taking values from 0 to 4. This is the most common use case.
2. Range with Two Arguments: range(start, stop)
With two arguments, you define a starting point. range(start, stop) begins at the 'start' number. It ends before the 'stop' number.
# Loop from 5 to 9
for i in range(5, 10):
print(i)
5
6
7
8
9
This is useful when you don't want to start from zero. It gives you more control over the iteration.
3. Range with Three Arguments: range(start, stop, step)
With three arguments, you add a step value. range(start, stop, step) changes how much the counter increases or decreases.
A positive step counts upwards. A negative step counts backwards.
# Count up by 2s
print("Counting up by 2:")
for i in range(0, 10, 2):
print(i)
# Count down from 5
print("\nCounting down:")
for i in range(5, 0, -1):
print(i)
Counting up by 2:
0
2
4
6
8
Counting down:
5
4
3
2
1
The step parameter is powerful for creating custom sequences. It can skip numbers or reverse the order.
Practical Examples of For Loop with Range
Let's move from theory to practice. Here are common tasks solved with for loops and range().
Example 1: Creating a Multiplication Table
This example prints the multiplication table for the number 3.
number = 3
print(f"Multiplication table for {number}:")
for multiplier in range(1, 11): # From 1 to 10
result = number * multiplier
print(f"{number} x {multiplier} = {result}")
Multiplication table for 3:
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30
Example 2: Iterating Through List Indices
Often you need the index of an item, not just the item itself. Use range() with the len() function.
colors = ["red", "green", "blue", "yellow"]
print("List items with their index:")
for index in range(len(colors)):
print(f"Index {index}: {colors[index]}")
List items with their index:
Index 0: red
Index 1: green
Index 2: blue
Index 3: yellow
This technique is essential for modifying list items in place. For more on iterating sequences, explore our guide on Python Range Function Guide: Syntax & Examples.
Example 3: Building a Pattern with Nested Loops
Nested loops use one loop inside another. They are great for grids and patterns.
# Print a simple 5x5 square of asterisks
print("5x5 Square Pattern:")
for row in range(5):
for column in range(5):
print("*", end=" ") # Print on the same line
print() # Move to the next line after each row
5x5 Square Pattern:
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
Nested loops demonstrate the real power of automation. The outer loop controls rows. The inner loop controls columns.
Common Mistakes and Best Practices
Beginners often make a few common errors. Let's address them.
Mistake 1: Forgetting range() is exclusive.range(5) gives numbers 0-4, not 0-5. Always remember the stop value is not included.
Mistake 2: Using range() on non-iterative tasks. If you need to process each item in a list, loop directly over the list. Use range(len(list)) only when you need the index.
Best Practice: Use descriptive variable names. Use names like index, row, or step_counter instead of just i. It makes your code readable.
Best Practice: Combine with enumerate(). For getting both index and value from a list, enumerate() is often cleaner than range(len()).
Conclusion
The combination of for loops and the range() function is a cornerstone of Python programming. It provides precise control over iteration.
You learned the three forms of range(): with stop, start-stop, and start-stop-step. You saw practical examples for creating sequences, accessing indices, and building patterns.
Mastering this concept will help you write efficient, clean, and powerful Python code. Practice by modifying the examples. Try creating a countdown timer or a pattern of numbers.
For a deeper dive into the function itself, revisit our Python Range Function Guide: Syntax & Examples. Keep coding and experimenting. This simple tool will automate countless tasks in your projects.