In Python, the range() function generates a sequence of numbers, often used in loops for iteration. By default, it creates numbers starting from 0 up to but not including a specified stop value. You can also reverse the sequence with reversed(). If you need to count backwards, then you can use a negative step, like range(start, stop, -1), which counts down from start to stop.
The range() function is not just about iterating over numbers. It can also be used in various programming scenarios beyond simple loops. By mastering range(), you can write more efficient and readable Python code. Explore how range() can simplify your code and when alternatives might be more appropriate.
By the end of this tutorial, you’ll understand that:
- A range in Python is an object representing an interval of integers, often used for looping.
- The
range()function can be used to generate sequences of numbers that can be converted to lists. for i in range(5)is a loop that iterates over the numbers from 0 to 4, inclusive.- The range parameters
start,stop, andstepdefine where the sequence begins, ends, and the interval between numbers. - Ranges can go backward in Python by using a negative step value and reversed by using
reversed().
A range is a Python object that represents an interval of integers. Usually, the numbers are consecutive, but you can also specify that you want to space them out. You can create ranges by calling range() with one, two, or three arguments, as the following examples show:
>>> list(range(5)) [0, 1, 2, 3, 4] >>> list(range(1, 7)) [1, 2, 3, 4, 5, 6] >>> list(range(1, 20, 2)) [1, 3, 5, 7, 9, 11, 13, 15, 17, 19] In each example, you use list() to explicitly list the individual elements of each range. You’ll study these examples in more detail later on.
A range can be an effective tool. However, throughout this tutorial, you’ll also explore alternatives that may work better in some situations. You can click the link below to download the code that you’ll see in this tutorial:
Get Your Code: Click here to download the free range() sample code that shows you how to represent numerical ranges in Python.
Python for Beginners Live Course: Want more practice with loops, ranges, and the rest of Python’s building blocks? In Real Python’s Python for Beginners live course, you build real programs with an instructor and a small group of fellow learners. Join the waitlist to hear about the next dates.
Construct Numerical Ranges
In Python, range() is built in. This means that you can always call range() without doing any preparations first. Calling range() constructs a range object that you can put to use. Later, you’ll see practical examples of how to use range objects.
You can provide range() with one, two, or three integer arguments. This corresponds to three different use cases:
- Ranges counting from zero
- Ranges of consecutive numbers
- Ranges stepping over numbers
You’ll learn how to use each of these next.
Count From Zero
When you call range() with one argument, you create a range that counts from zero and up to, but not including, the number you provided:
>>> range(5) range(0, 5) Here, you’ve created a range from zero to five. To see the individual elements in the range, you can use list() to convert the range to a list:
>>> list(range(5)) [0, 1, 2, 3, 4] Inspecting range(5) shows that it contains the numbers zero, one, two, three, and four. Five itself is not a part of the range. One nice property of these ranges is that the argument, 5 in this case, is the same as the number of elements in the range.
Count From Start to Stop
You can call range() with two arguments. The first value will be the start of the range. As before, the range will count up to, but not include, the second value:
>>> range(1, 7) range(1, 7) The representation of a range object just shows you the arguments that you provided, so it’s not super helpful in this case. You can use list() to inspect the individual elements:
Read the full article at https://realpython.com/python-range/ »
[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]
