codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐍

Python Programming

16 / 68 topics
14Python if...else Statement15Python Match Statement16Python for Loop & range()17Python while Loop18Python break, continue, and pass
Tutorials/Python Programming/Python for Loop & range()
🐍Python Programming

Python for Loop & range()

Updated 2026-05-15
20 min read

Python for Loop & range()

In this tutorial, you'll learn how to use the for loop in Python along with the versatile range() function. These tools are fundamental for iterating over sequences like lists and strings, controlling iteration steps, and tracking indices. Understanding these concepts is crucial for writing efficient and readable code.

Introduction

The for loop is a control flow statement that allows you to execute a block of code repeatedly for each item in a sequence (like a list or string). The range() function generates a sequence of numbers, which can be used to control the number of iterations in a for loop. Together, they provide powerful ways to handle repetitive tasks and process data.

Iterating Over Sequences

Iterating Over Lists

You can use a for loop to iterate over each element in a list. Here's a simple example:

iterate_list.py
1fruits = ["apple", "banana", "cherry"]
2
3for fruit in fruits:
4 print(fruit)
Output
apple
banana
cherry

In this example, the for loop goes through each item in the fruits list and prints it.

Iterating Over Strings

Strings are sequences of characters, so you can iterate over them as well:

iterate_string.py
1greeting = "Hello"
2
3for char in greeting:
4 print(char)
Output
H
e
l
l
o

Here, the loop iterates over each character in the string "Hello" and prints it.

Using the range() Function

The range() function is used to generate a sequence of numbers. It can take one, two, or three arguments:

  • range(stop): Generates numbers from 0 up to, but not including, stop.
  • range(start, stop): Generates numbers from start up to, but not including, stop.
  • range(start, stop, step): Generates numbers from start up to, but not including, stop, incrementing by step.

Basic Usage

Here's a basic example using range():

basic_range.py
1for i in range(5):
2 print(i)
Output
0
1
2
3
4

This loop prints numbers from 0 to 4.

Specifying Start and Stop

You can specify both the start and stop values:

start_stop_range.py
1for i in range(2, 7):
2 print(i)
Output
2
3
4
5
6

This loop prints numbers from 2 to 6.

Using Step

You can also specify a step value:

step_range.py
1for i in range(0, 10, 2):
2 print(i)
Output
0
2
4
6
8

This loop prints even numbers from 0 to 8.

Tracking Indices with enumerate()

Sometimes you need both the index and the value of each item in a sequence. The enumerate() function can help with this:

enumerate_example.py
1fruits = ["apple", "banana", "cherry"]
2
3for index, fruit in enumerate(fruits):
4 print(f"Index {index}: {fruit}")
Output
Index 0: apple
Index 1: banana
Index 2: cherry

In this example, enumerate() returns both the index and the value of each item in the list.

Practical Example

Let's create a practical example that combines these concepts. We'll write a program to print the squares of numbers from 1 to 5:

squares_example.py
1for i in range(1, 6):
2 square = i * i
3 print(f"The square of {i} is {square}")
Output
The square of 1 is 1
The square of 2 is 4
The square of 3 is 9
The square of 4 is 16
The square of 5 is 25

This program uses a for loop with range() to iterate over numbers from 1 to 5, calculates their squares, and prints the results.

Summary

  • For Loop: Used to iterate over sequences like lists and strings.
  • Range Function: Generates sequences of numbers for controlling iterations. Can take one, two, or three arguments.
  • Enumerate Function: Provides both index and value when iterating over a sequence.

By mastering these concepts, you'll be able to write more efficient and readable code that handles repetitive tasks effectively.

What's Next?

In the next tutorial, we'll explore the while loop, which allows for more flexible control over iterations compared to the for loop. This will give you even more tools to handle various programming scenarios. Stay tuned!


PreviousPython Match StatementNext Python while Loop

Recommended Gear

Python Match StatementPython while Loop