Python for Loop with range() Function – A Beginner Guide

🐍 Introduction

When writing a Python program, we often need to perform the same task several times. For example, we may want to print numbers from 1 to 10, display a multiplication table, or repeat a calculation.

Instead of writing the same statement repeatedly, Python provides loops. A loop allows us to execute a block of code several times.

💡 Think About It
What if we want Python to perform the same task several times?

One of the most commonly used loops in Python is the for loop. It is especially useful when we know how many times we want to repeat an operation.

▶ A simple example
for i in range(5):
    print(i)
▶ Output
0
1
2
3
4
📌 What happened? The for loop takes each number produced by range(5) and stores it in the variable i. The print() statement displays the value.

In this tutorial, we will learn the basics of the for loop and understand how the range() function helps us control the numbers used by the loop.

🚀 In this tutorial, you will learn
  • What a for loop is
  • Basic for loop syntax
  • How the range() function works
  • The basic forms of range()
  • Simple programs using for and range()

1. What is a for Loop?

A for loop is used to repeat a block of code for each value in a sequence. It saves us from writing the same code again and again.

▶ Simple example
for i in range(5):
    print(i)
▶ Output
0
1
2
3
4
📌 What is happening?

The for loop takes one value at a time and executes the indented print(i) statement. This continues until all the values have been processed.

🔎 Visual Understanding

A simple way to understand a loop is to think of it as a repeated sequence of actions:

🔄 Get a value Run the code Get the next value
▶ How the loop works
i = 0 → print(0)
i = 1 → print(1)
i = 2 → print(2)
i = 3 → print(3)
i = 4 → print(4)
⭐ Beginner Tip Don’t worry about understanding every detail of range() yet. We will learn how range() works step by step in the next section.

Now that we understand the basic idea of a for loop, let’s see how its basic syntax is written in Python.

2. Basic Syntax of a for Loop

The basic structure of a Python for loop is simple. It consists of a for statement followed by the code that should be repeated.

▶ Basic syntax
for variable in range(…):
    statement

Let’s understand the main parts of this syntax:

Part
Meaning
for
Starts the loop
variable
Stores the current value
in
Gets values from range()
range()
Produces a sequence of numbers
statement
Code executed repeatedly
⭐ Beginner Tip The code inside the loop must be indented. Indentation tells Python which statements belong to the loop.

Now that we know the basic syntax, let’s understand the range() function and see how it controls the values used by the loop.

3. Understanding the range() Function

The range() function is commonly used with a for loop to generate a sequence of numbers.

Let’s begin with the simplest form of range().

range(stop)

When range() has only one value, that value is the stop value. Python starts counting from 0.

▶ Example
for i in range(5):
    print(i)
▶ Output
0
1
2
3
4
⭐ Important Rule
range(5) starts from 0 and stops before 5. Therefore, 5 is not included.

🔎 Visual Understanding

Think of range(5) as moving through the numbers from 0 up to, but not including, 5.

range(5)
0   1   2   3   4   5
✓   ✓   ✓   ✓   ✓   ✗
📌 Remember: The last value, 5, is the stop value, so it is not included in the sequence.
💡 Quick view
range(5)  →  0, 1, 2, 3, 4

The range() function can also start from a number other than 0. Let’s look at the next form:

4. range(start, stop)

The second form of range() allows us to specify both a starting value and a stopping value.

💡 Syntax: range(start, stop)
▶ Example
for i in range(2, 7):
    print(i)
▶ Output
2
3
4
5
6
▶ start
The first value in the sequence.
▶ stop
The value where the sequence stops.

So Python generates:

2, 3, 4, 5, 6
⭐ Remember
The start value is included, but the stop value is excluded.

🔎 Visual Understanding

range(2, 7)
2   3   4   5   6   7
✓   ✓   ✓   ✓   ✓   ✗
📌 In this example: Python starts at 2 and generates the numbers up to 6. It stops before 7.

Next, let’s see how the step value can be used to control how much the numbers change each time.

5. range(start, stop, step)

The third form of range() lets us specify how much the value should change after each iteration. This value is called the step.

💡 Syntax: range(start, stop, step)
▶ Example 1
for i in range(2, 10, 2):
    print(i)
▶ Output
2
4
6
8
🔎 Understanding the three values
2 → starting value
10 → stopping value
2 → increase by 2 each time
2   →   4   →   6   →   8
Increase by 2 each time
📌 Remember: The start value is included, but the stop value is excluded. The step tells Python how much to change the value each time.

Another Simple Example

The step does not have to be 2. For example, a step of 3 increases the value by 3 each time.

▶ Example 2
for i in range(1, 10, 3):
    print(i)
▶ Output
1
4
7
⭐ Quick Understanding
range(1, 10, 3) produces 1, 4, 7. After each value, Python adds 3 and stops before 10.

Now that we know the three basic forms of range(), let’s use them to write some simple Python programs.

6. Three Forms of range()

Python provides three basic ways to use the range() function. Here’s a quick summary:

Form
Example
Values
range(stop)
range(5)
0, 1, 2, 3, 4
range(start, stop)
range(2, 6)
2, 3, 4, 5
range(start, stop, step)
range(2, 10, 2)
2, 4, 6, 8
⭐ Quick Reminder
In all three forms, the stop value is excluded. The step is used to control how the value changes.
💡 Easy way to remember
One value → stop only   |   Two values → start, stop   |   Three values → start, stop, step

Now let’s put these concepts into practice with some simple for loop programs.

7. Simple Programs Using for + range()

Now that we understand the basic forms of range(), let’s use them in some simple Python programs.

💡 Tip: Try running each program yourself and observe the output.
Program 1 – Print numbers from 1 to 10
▶ Python
for i in range(1, 11):
    print(i)
Program 2 – Print even numbers
▶ Python
for i in range(2, 11, 2):
    print(i)
Program 3 – Print odd numbers
▶ Python
for i in range(1, 10, 2):
    print(i)
Program 4 – Print numbers from 10 to 1

Here we use a negative step to count backwards.

▶ Python
for i in range(10, 0, -1):
    print(i)
Program 5 – Find the sum of numbers
▶ Python
total = 0

for i in range(1, 6):
    total = total + i

print(“Sum =”, total)
Output: Sum = 15
Program 6 – Multiplication table
▶ Python
num = 5

for i in range(1, 11):
    print(num, “×”, i, “=”, num * i)
⭐ Practice Tip Change the numbers in these programs and run them again. This is a simple way to understand how range() controls the loop.

These examples cover some common beginner uses of for loops with range(). Next, let’s look at some common mistakes beginners may encounter.

📌 Remember This

Here are a few important points to remember when using for loops with range().

💡 Remember: In range(start, stop), the stop value is not included.
⭐ Beginner Tip: When only one number is given to range(), Python starts counting from 0.
🔎 Remember: In range(start, stop, step), the step tells Python how much the value changes each time.
⚠ Common Mistake: Remember to put a colon : at the end of the for statement and indent the code inside the loop.
🧠 Quick Summary
range(stop) → starts from 0
range(start, stop) → starts from start
range(start, stop, step) → changes by step
stop → always excluded

8. Practice Questions

Now it’s your turn! Try these simple exercises using for loops and range().

💡 Practice Tip Try writing and running each program yourself before looking for a solution.
📝 Practice 1
Write a Python program to print numbers from 1 to 20.
📝 Practice 2
Print the even numbers from 2 to 20.
📝 Practice 3
Print the odd numbers from 1 to 19.
📝 Practice 4
Print numbers from 10 down to 1.
📝 Practice 5
Write a Python program to print the multiplication table of 7.
⭐ Bonus Challenge
Print the numbers from 5 to 50 with a step of 5.
📚 Want to check your answers?
The solutions can be provided separately so that you can first try the exercises on your own.

Once you are comfortable with these examples, you are ready to explore more ways of using for loops in Python.

9. Practice Questions – Solutions

Here are the solutions to the practice questions. Compare your programs with these simple solutions and try to understand how range() is used in each example.

💡 Tip: Don’t just compare the output. Look at the start, stop, and step values.
✅ Practice 1 – Numbers from 1 to 20
▶ Solution
for i in range(1, 21):
    print(i)
Why 21? The stop value is excluded, so the sequence ends at 20.
✅ Practice 2 – Even numbers from 2 to 20
▶ Solution
for i in range(2, 21, 2):
    print(i)
Step = 2 skips every alternate number.
✅ Practice 3 – Odd numbers from 1 to 19
▶ Solution
for i in range(1, 20, 2):
    print(i)
Step = 2 generates only the odd numbers.
✅ Practice 4 – Numbers from 10 down to 1
▶ Solution
for i in range(10, 0, -1):
    print(i)
Step = -1 makes the loop count backwards.
✅ Practice 5 – Multiplication table of 7
▶ Solution
num = 7

for i in range(1, 11):
    print(num * i)
Output: 7, 14, 21, 28, 35, 42, 49, 56, 63, 70
⭐ Bonus Challenge – Solution
▶ Solution
for i in range(5, 51, 5):
    print(i)
Output: 5, 10, 15, 20, 25, 30, 35, 40, 45, 50
🧠 Key Takeaway
The main idea is to choose the correct start, stop, and step values for range(). Remember that the stop value is excluded.

🎯 Conclusion

In this tutorial, you learned how to use Python’s for loop with the range() function. You also learned its three basic forms and used them in simple programs.

⭐ Keep practicing with different values of range() to become comfortable with loops.

🚀 What’s next? In the next tutorials, we’ll explore more ways to use for loops in Python.

Gopal Krishna

Hey Engineers, welcome to the award-winning blog,Engineers Tutor. I'm Gopal Krishna. a professional engineer & blogger from Andhra Pradesh, India. Notes and Video Materials for Engineering in Electronics, Communications and Computer Science subjects are added. "A blog to support Electronics, Electrical communication and computer students".

Leave a Reply

Your email address will not be published. Required fields are marked *

Translate »