W3Schools Python For Loop Range Tutorial Guide

Logan Jul 13, 2026

When it comes to programming, loops are fundamental constructs that allow us to execute a block of code repeatedly. In Python, loops are implemented using the `for` loop and `while` loop. Today, we'll focus on the `for` loop and its interaction with the `range` function, a staple in many W3Schools Python tutorials.

Python for Loops: The Pythonic Way – Real Python
Python for Loops: The Pythonic Way – Real Python

The `for` loop in Python is used to iterate over a sequence (list, tuple, string, etc.) or other iterable objects. This is particularly useful when we know how many iterations we need beforehand. Enter the `range` function, which generates a sequence of numbers starting from 0 by default, increments by 1 (also default), and stops before a specified number.

Python Loops Cheat Sheet
Python Loops Cheat Sheet

Understanding the `range` Function

The `range` function in Python returns a sequence of numbers starting from the first input parameter (default is 0) up to but not including the second input parameter (stop parameter). The third, optional parameter is the step size (default is 1).

How to use while loops in Python
How to use while loops in Python

Syntax: `range(stop)` or `range(start, stop[, step])`

Generating a Sequence

the instructions for how to use python loops
the instructions for how to use python loops

The most common use of `range` is to generate a sequence of numbers. By default, `range` starts from 0 and increments by 1:

```python for i in range(5): print(i) ```

Output:

``` 0 1 2 3 4 ```

Customizing the Sequence

Follow @CodeWithAdya for more Python notes, coding tips, and beginner-friendly content 💻💜 #Python
Follow @CodeWithAdya for more Python notes, coding tips, and beginner-friendly content 💻💜 #Python

We can customize the sequence by changing the `start`, `stop`, and `step` parameters:

```python # Start from 2, stop before 10, step size is 2 for i in range(2, 10, 2): print(i) ```

Output:

``` 2 4 6 8 ```

Using `range` with `for` Loops

18 Python while Loop Examples and Exercises
18 Python while Loop Examples and Exercises

The `range` function is commonly used with `for` loops to iterate over a sequence of numbers. This is particularly useful when you know how many times you want to loop:

Iterating over Indices

python Loops for loop and while loop
python Loops for loop and while loop
Coding For Beginners Python - Learn the Basics - Loops
Coding For Beginners Python - Learn the Basics - Loops
While loop in Python Easy to learn 🤩
While loop in Python Easy to learn 🤩
12.coding with range and for loop in python
12.coding with range and for loop in python
For Loop, While Loop & Nested Loops Cheat Sheet
For Loop, While Loop & Nested Loops Cheat Sheet
a white background with black and red text that reads, python loops for loop printing
a white background with black and red text that reads, python loops for loop printing
Python Loops Explained for Beginners | Learn for & while Loops
Python Loops Explained for Beginners | Learn for & while Loops
30-Day Python Learning Plan for Beginners | Complete Python Roadmap to Learn Coding Fast
30-Day Python Learning Plan for Beginners | Complete Python Roadmap to Learn Coding Fast
For Loop & While Loop Explained | Python for Beginners
For Loop & While Loop Explained | Python for Beginners
Python Learning Path for Beginners 2026 🚀
Python Learning Path for Beginners 2026 🚀
Learn Python in 10 days
Learn Python in 10 days
Python While Loop | How to Use While Loops?| Continue | Break ⋆ IpCisco
Python While Loop | How to Use While Loops?| Continue | Break ⋆ IpCisco
Python Loops - Learn one of the most powerful concepts in programming - TechVidvan
Python Loops - Learn one of the most powerful concepts in programming - TechVidvan
🐍 Python Loops Explained | For Loop, While Loop, Break, Continue & Pass
🐍 Python Loops Explained | For Loop, While Loop, Break, Continue & Pass
For Loop vs While Loop in Python | Key Differences Explained
For Loop vs While Loop in Python | Key Differences Explained
Python Loops with Lists Explained
Python Loops with Lists Explained
Python Notes for Beginners (Easy + Quick Guide)
Python Notes for Beginners (Easy + Quick Guide)
Python Loops - Freely execute any statement n number of times
Python Loops - Freely execute any statement n number of times

Using `range` with `for` loops allows us to iterate over the indices of a sequence. This can be useful when you want to access elements of a list, tuple, etc., based on their index:

```python fruits = ['apple', 'banana', 'cherry'] for i in range(len(fruits)): print("I like", fruits[i]) ```

Output:

``` I like apple I like banana I like cherry ```

Enumerating Sequences

Using the built-in `enumerate` function along with `range` allows us to iterate over both the index and value of a sequence:

```python fruits = ['apple', 'banana', 'cherry'] for i, fruit in enumerate(fruits): print(f"I like {fruit}, which is number {i+1}") ```

Output:

``` I like apple, which is number 1 I like banana, which is number 2 I like cherry, which is number 3 ```

Mastering the `for` loop and `range` function in Python opens up a world of possibilities in terms of iterating over sequences and performing repetitive tasks. By understanding and practicing these concepts, you're one step closer to becoming a proficient Python programmer. Happy coding!