Master w3schools Python Range Loop with Easy Examples

Logan Jul 13, 2026

Welcome to our in-depth guide on leveraging the power of Python's range function with loops! The W3Schools Python range loop is a fundamental aspect of Python programming that enables you to automate repetitive tasks effortlessly.

Python Loops Cheat Sheet for Beginners
Python Loops Cheat Sheet for Beginners

The beauty of Python's range function lies in its simplicity and efficiency. By using range in combination with loops, you can iterate over a sequence of numbers, lists, strings, or any iterable, making it a core component of modern programming.

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

Understanding Python's Range Function

The range function generates a sequence of numbers starting from the supplied start number, ending at the supplied stop number (excluding the stop number), with a fixed step increment.

9 Python Loop Tricks Every Beginner Should Learn (Cheat Sheet)
9 Python Loop Tricks Every Beginner Should Learn (Cheat Sheet)

By default, the increment step is 1, but you can adjust it to create patterns, skip numbers, or reverse the sequence. Here's a simple example:

Basic Syntax of Range

Python For Loop Explained 🐍 | Complete Beginner Cheat Sheet
Python For Loop Explained 🐍 | Complete Beginner Cheat Sheet

Here's the basic syntax of Python's range function:

range(start, stop, step)

  • start: The starting number of the sequence (default is 0).
  • stop: The ending number of the sequence.
  • step: The increment or decrement value (default is 1).

For instance, range(5) generates numbers from 0 to 4. Let's explore this with a simple for loop:

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

Iterating Over a Range

To iterate over the range, we use a for loop. Each iteration picks up the next number from the sequence generated by the range function:

for num in range(5): print(num)

This will print numbers from 0 to 4 on separate lines.

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

Looping with Range and Indexing

Python's range function pairs perfect with for loops to iterate over an iterable. Often, you'd want to know the index of the current item in the iteration. This is where the enumerate function comes in handy:

While loop in Python Easy to learn 🀩
While loop in Python Easy to learn 🀩
Python Loops Cheat Sheet
Python Loops Cheat Sheet
python Loops for loop and while loop
python Loops for loop and while loop
Loops in Python πŸ’»
Loops in Python πŸ’»
Python Loops Explained for Beginners (Easy Guide)
Python Loops Explained for Beginners (Easy Guide)
Python list loop tutorial infographic
Python list loop tutorial infographic
Python For Loop vs While Loop Explained for Beginners
Python For Loop vs While Loop Explained for Beginners
Coding For Beginners Python - Learn the Basics - Loops
Coding For Beginners Python - Learn the Basics - Loops
18 Python while Loop Examples and Exercises
18 Python while Loop Examples and Exercises
Master Python Loops: The Ultimate Beginner’s Cheat Sheet πŸ”„πŸ
Master Python Loops: The Ultimate Beginner’s Cheat Sheet πŸ”„πŸ
Python Loops - Learn one of the most powerful concepts in programming - TechVidvan
Python Loops - Learn one of the most powerful concepts in programming - TechVidvan
Codin For Beginners Python - Learn the Basics - Loops
Codin For Beginners Python - Learn the Basics - 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
Python Loops Complete Cheatsheet πŸŒ€ | for, while, break, continue, nested loops & patterns
Python Loops Complete Cheatsheet πŸŒ€ | for, while, break, continue, nested loops & patterns
Python while Loop: Python Tutorial on while Loop with Examples
Python while Loop: Python Tutorial on while Loop with Examples
Python Loops Explained (For Loop & While Loop Guide)
Python Loops Explained (For Loop & While Loop Guide)
a screenshot of a black screen with text that reads 8 ways to make your python loops run faster
a screenshot of a black screen with text that reads 8 ways to make your python loops run faster
Python For Loop | How To Use Python For Loops? | For Loops ⋆
Python For Loop | How To Use Python For Loops? | For Loops ⋆
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

Using Enumerate with Range

Enumerate returns a tuple containing a count and the values obtained from iterating over the object. Here's how to use it with range:

for i, num in enumerate(range(5)): print(i, num)

This will print both the index and the number from 0 to 4, like so:

(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)

Looping with Offsets

Sometimes, you need to start your loop from a specific index, exclusive of the stop number. You can use negative range values for this:

for i in range(4, -1, -1): print(i)

This will print numbers from 4 to 0 in descending order.

Understanding and mastering the Python range loop is essential for any programmer looking to automate tasks and boost productivity. Happy coding, and may your loops be wise and your ranges well contained!