Python range(): Represent Numerical Ranges
by:
blow post content copied from Real Python
click here to view original post
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 soon.
In this tutorial, you’ll learn how you can:
- Create
range
objects that represent ranges of consecutive integers - Represent ranges of spaced-out numbers with a fixed step
- Decide when
range
is a good solution for your use case - Avoid
range
in most loops
A range
can sometimes be a powerful 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 sample code that shows you how to represent numerical ranges in Python.
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:
>>> list(range(1, 7))
[1, 2, 3, 4, 5, 6]
Observe that range(1, 7)
starts at one and includes the consecutive numbers up to six. Seven is the limit of the range and isn’t included. You can calculate the number of elements in a range by subtracting the start value from the end value. In this example, there are 7 - 1 = 6 elements.
Count From Start to Stop While Stepping Over Numbers
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 ]
January 10, 2024 at 07:30PM
Click here for more details...
=============================
The original post is available in Real Python by
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.
============================
Post a Comment