12-12-2020, 10:22 PM
Python range() Function — A Helpful Illustrated Guide
The Python function creates an iterable of subsequent integers within a given range of values. You can pass either only a argument in which case the object will include all integers from to (excluded). Or you can pass , , and arguments in which case the range object will go from to using the given size. For example, results in and results in .
Here are some examples of how to use the built-in function:
Note that in any case, a range object is returned. The range object is an iterable of values—but the values are only generated as they’re actually needed. You can use the asterisk operator to unpack all values into the print function with. Python waits as long as possible to generate the values of the iterable.
Syntax Range Function
You can use the function with three different argument lists: (i) with the argument only, (ii) with the and arguments, or (iii) with the , , and arguments.
Arguments |
|
An integer defining the first element of the iterable
|
|
An integer defining the last element. However, this element is not included in the r iterable.
|
|
An integer defining the difference between two subsequent elements in the iterable.
Return Value |
|
Returns an iterable range object that allows you to iterate over all values from to using the size.
Interesting fact: the “function” is technically not a normal function but a constructor method of the class. Thus, it creates a new range object.
How Math Genius Gauss Hacked His Teacher’s Exercise With the Range Function
Do you know the following story of the brilliant mathematician Carl Friedrich Gauss? When 8-year old Gauss went to school, his math teacher sought a few minutes of breathing pause. He told his class to solve the problem of adding all subsequent numbers from 1-100:.
But as little Gauss promptly reported the solution, the short pause was over before it began.
Surprised (and a bit grumpy as the story goes), the teacher asked the boy how he had come up with a solution so quickly. Gauss explained his simple solution. He organized the sequence into pairs of numbers each summing up to 101:. There are 50 such pairs, so the total result was .
Yet, the modern-time little Gauss would be even lazier. He would type the following one-linerinto his mobile Python app:.
The function returns a sequence starting from the first value (inclusive) and ending in the second value (exclusive). The sum function sums up the values of this sequence. Combining both functions sums up the sequence from 1-100—faster than the brilliant Carl Friedrich Gauss.
Python range() With One Argument Stop
You can use the function with one argument . In this case, the range object goes from to the argument (excluded) by using the default step size of one.
![[Image: range_1-1-1024x576.jpg]](https://blog.finxter.com/wp-content/uploads/2020/12/range_1-1-1024x576.jpg)
Here’s the example:
The output is:
Python range() With Two Arguments Start + Stop
You can use the function with two arguments and . In this case, the range object goes from to the integer value (excluded) by using the default step size of one.
![[Image: range2-1024x576.jpg]](https://blog.finxter.com/wp-content/uploads/2020/12/range2-1024x576.jpg)
Here’s the example:
The output is:
Python range() With Three Arguments Start + Stop + Step
You can use the function with three arguments , , and . In this case, the object goes from to the integer value (excluded) by using the default step size of .
![[Image: range3-1024x576.jpg]](https://blog.finxter.com/wp-content/uploads/2020/12/range3-1024x576.jpg)
Here’s the example:
The output is:
Interactive Shell Exercise About The Range Function
The following code snippet matches men with women—the idea is to match the i-th man with the i-th woman, assuming that both lists have the same size. How to change the code to accomplish this task?
https://trinket.io/embed/python/5ab4ae4f21
Exercise: Replace the placeholder in the code to match the i-th man with the i-th woman!
You’ll find the solution… after the advertisement!![[Image: 1f642.png]](https://s.w.org/images/core/emoji/13.0.1/72x72/1f642.png)
But before we move on, I’m excited to present you my brand-new Python book Python One-Liners (Amazon Link).
If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!
![[Image: image-1.png]](https://blog.finxter.com/wp-content/uploads/2020/02/image-1.png)
The book is released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).
Link: https://nostarch.com/pythononeliners
Solution: The following code solves the exercise.
The idea is to use the function to determine the argument automatically with . Note that , , and , and would all solve the problem equally well.
Python range() With Negative Step Size
You can also use the range() function with negative step size. The meaning is “move from right to the left using the negative step size as the difference between two subsequent values. In this case, the start argument should be larger than the stop argument.
![[Image: range4_neg-1024x576.jpg]](https://blog.finxter.com/wp-content/uploads/2020/12/range4_neg-1024x576.jpg)
Here’s an example:
The output is:
Note that the argument is still not included in the object.
Range Puzzle
Puzzles are a great and effective way to improve your Python skills. Can you solve this range puzzle?
What is the output of this code snippet?
You can check whether you solved this puzzle correctly, and determine whether you’re a master coder on our Puzzle app Finxter.com:
![[Image: image-21-1024x526.png]](https://blog.finxter.com/wp-content/uploads/2020/12/image-21-1024x526.png)
Are you a master coder?
Test your skills now!
Summary
The Python function creates an iterable of subsequent integers within a given range of values.
You can pass either only a argument in which case the object will include all integers from to (excluded). For example, results in :
As an alternative, you can pass, , and arguments in which case the range object will go from to using the given size. For example, results in :
I hope you enjoyed the article! To improve your Python education, you may want to join the popular free Finxter Email Academy:
Do you want to boost your Python skills in a fun and easy-to-consume way? Consider the following resources and become a master coder!
Where to Go From Here?
Enough theory, let’s get some practice!
To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
Practice projects is how you sharpen your saw in coding!
Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?
Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.
Join the free webinar now!
The post Python range() Function — A Helpful Illustrated Guide first appeared on Finxter.
The Python
Code:
range()Code:
stopCode:
rangeCode:
stopCode:
startCode:
stopCode:
stepCode:
startCode:
stepCode:
stepCode:
range(3)Code:
0, 1, 2Code:
range(2, 7, 2)Code:
2, 4, 6Here are some examples of how to use the
Code:
range()Code:
>>> range(10)
range(0, 10)
>>> print(range(10))
range(0, 10)
>>> print(*range(10))
0 1 2 3 4 5 6 7 8 9
>>> range(1, 10, 3)
range(1, 10, 3)
>>> print(*range(1, 10, 3))
1 4 7Note that in any case, a range object is returned. The range object is an iterable of values—but the values are only generated as they’re actually needed. You can use the asterisk operator to unpack all values into the print function with
Code:
print(*range(10))Syntax Range Function
You can use the
Code:
range()Code:
stopCode:
startCode:
stopCode:
startCode:
stopCode:
stepCode:
Syntax:
range(stop)
range(start, stop)
range(start, stop, step)Arguments |
Code:
startAn integer defining the first element of the
Code:
range|
Code:
stopAn integer defining the last element. However, this element is not included in the r
Code:
ange|
Code:
stepAn integer defining the difference between two subsequent elements in the
Code:
rangeReturn Value |
Code:
rangeReturns an iterable range object that allows you to iterate over all values from
Code:
startCode:
stepCode:
stepInteresting fact: the
Code:
range()Code:
rangeHow Math Genius Gauss Hacked His Teacher’s Exercise With the Range Function
Do you know the following story of the brilliant mathematician Carl Friedrich Gauss? When 8-year old Gauss went to school, his math teacher sought a few minutes of breathing pause. He told his class to solve the problem of adding all subsequent numbers from 1-100:
Code:
1+2+3+...+100But as little Gauss promptly reported the solution, the short pause was over before it began.
Surprised (and a bit grumpy as the story goes), the teacher asked the boy how he had come up with a solution so quickly. Gauss explained his simple solution. He organized the sequence into pairs of numbers each summing up to 101:
Code:
1+100,2+99,3+98,...,50+51Code:
50*101=5050Yet, the modern-time little Gauss would be even lazier. He would type the following one-linerinto his mobile Python app:
Code:
sum(range(1,101))The
Code:
range()Python range() With One Argument Stop
You can use the
Code:
range()Code:
stopCode:
start=0Code:
stop![[Image: range_1-1-1024x576.jpg]](https://blog.finxter.com/wp-content/uploads/2020/12/range_1-1-1024x576.jpg)
Here’s the example:
Code:
for i in range(5): print(i)The output is:
Code:
0
1
2
3
4You can use the
Code:
range()Code:
startCode:
stopCode:
startCode:
stop![[Image: range2-1024x576.jpg]](https://blog.finxter.com/wp-content/uploads/2020/12/range2-1024x576.jpg)
Here’s the example:
Code:
for i in range(1, 5): print(i)The output is:
Code:
1
2
3
4You can use the
Code:
range()Code:
startCode:
stopCode:
stepCode:
rangeCode:
startCode:
stopCode:
step![[Image: range3-1024x576.jpg]](https://blog.finxter.com/wp-content/uploads/2020/12/range3-1024x576.jpg)
Here’s the example:
Code:
for i in range(1, 5, 2): print(i)The output is:
Code:
1
3Interactive Shell Exercise About The Range Function
The following code snippet matches men with women—the idea is to match the i-th man with the i-th woman, assuming that both lists have the same size. How to change the code to accomplish this task?
https://trinket.io/embed/python/5ab4ae4f21
Exercise: Replace the
Code:
XXXXYou’ll find the solution… after the advertisement!
![[Image: 1f642.png]](https://s.w.org/images/core/emoji/13.0.1/72x72/1f642.png)
But before we move on, I’m excited to present you my brand-new Python book Python One-Liners (Amazon Link).
If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!
![[Image: image-1.png]](https://blog.finxter.com/wp-content/uploads/2020/02/image-1.png)
The book is released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).
Link: https://nostarch.com/pythononeliners
Solution: The following code solves the exercise.
Code:
men = ['Bob', 'Carl', 'Frank']
women = ['Ann', 'Alice', 'Liz'] for i in range(len(men)): print(men[i] + ' dances with ' + women[i])The idea is to use the
Code:
len()Code:
stopCode:
range(len(men))Code:
range(len(women))Code:
range(3)Code:
range(0, 3)Code:
range(0, 3, 1)Python range() With Negative Step Size
You can also use the range() function with negative step size. The meaning is “move from right to the left using the negative step size as the difference between two subsequent values. In this case, the start argument should be larger than the stop argument.
![[Image: range4_neg-1024x576.jpg]](https://blog.finxter.com/wp-content/uploads/2020/12/range4_neg-1024x576.jpg)
Here’s an example:
Code:
for i in range(4,0,-2): print(i)The output is:
Code:
4
2Note that the
Code:
stopCode:
rangeRange Puzzle
Puzzles are a great and effective way to improve your Python skills. Can you solve this range puzzle?
Code:
# Python Puzzle
print(sum(range(0,7)))You can check whether you solved this puzzle correctly, and determine whether you’re a master coder on our Puzzle app Finxter.com:
![[Image: image-21-1024x526.png]](https://blog.finxter.com/wp-content/uploads/2020/12/image-21-1024x526.png)
Are you a master coder?
Test your skills now!
Summary
The Python
Code:
range()You can pass either only a
Code:
stopCode:
rangeCode:
stopCode:
range(3)Code:
0, 1, 2Code:
for i in range(3): print(i) '''
OUTPUT:
0
1
2 '''As an alternative, you can pass
Code:
startCode:
stopCode:
stepCode:
startCode:
stepCode:
stepCode:
range(2, 7, 2)Code:
2, 4, 6Code:
for i in range(2, 7, 2): print(i) '''
OUTPUT:
2
4
6 '''I hope you enjoyed the article! To improve your Python education, you may want to join the popular free Finxter Email Academy:
Do you want to boost your Python skills in a fun and easy-to-consume way? Consider the following resources and become a master coder!
Where to Go From Here?
Enough theory, let’s get some practice!
To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
Practice projects is how you sharpen your saw in coding!
Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?
Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.
Join the free webinar now!
The post Python range() Function — A Helpful Illustrated Guide first appeared on Finxter.

