Using range() to generate list of numbers in Python

problem

You’re learning Python and wish to combine range with for loop. In this post we will see an example of this combination and how useful it is.

SOLUTION

The range is a built-in function that generates a range type. For example a range of values, starting from 0. We can generate 5 numbers with the following code.

range.py
				
					r = range(5) # generate 5 numbers starting from 0
				
			

The numbers generated are from 0 (inclusive) to 5(exclusive). More specific: 0,1,2,3,4. If we try to print the range it won’t show any values but the following.

range.py
				
					r = range(5) # generate 5 numbers starting from 0
print(r)

				
			
OUTPUT​
range in python
Converting range to a list
range.py
				
					r = range(5) # generate 5 numbers starting from 0
nums = list(r) # convert range to a list of numbers
print(nums)

				
			
OUTPUT
range to list in Python
Converting range to a set
range.py
				
					r = range(5) # generate 5 numbers starting from 0
nums = set(r) # convert range to a set of numbers
print(nums)
				
			
output
Using a range on for loop

One of the benefits of range() is that it is iterable. This mean we can iterate over it by using a loop mechanism like for-loop. For example to print 10 numbers we can do the following:

range.py
				
					for num in range(10):
     print(num)
				
			

The code above will iterate over the range of 10 numbers and print the following:

output
for loop with range in Python
Parameters of range

By default range generates numbers from 0 to the number we pass as argument (exclusive). For example as we saw above, range(5) will generate values: 0,1,2,3,4. Each successor number in the range is incremented by 1. This is also added by default.

Changing the range

To create a custom range we can pass 2 parameters. The first parameter is the starting number and the second the ending number (exclusive). For example to generate numbers from 1 to 10 inclusive we can do the following:

range.py
				
					nums_1_to_10 = range(1,11)
				
			

If we wish to change the step (the number added to the successor) we can pass it as 3rd parameter

Generate even numbers using range

For example, let’s generate a range of all even numbers from 0 to 10.

range.py
				
					even = range(0,10,2) # generates 0,2,4,6,8

				
			

The 10 is exclusive so it won’t make it to the range.

Generate odd numbers using range

Let’s generate a range of odd numbers from 1 to 10.

range.py
				
					odd = range(1,10,2) # generates 1,3,5,7,9

				
			

If we wish to print them in a row as a list, we simply convert the range to list as follows:

range.py
				
					print(list(odd))
				
			
use of Range in synopsis

There are 3 ways of using range():

  1. range(a)        # generates numbers from 0 to a-1, incremented by 1
  2. range(a,b)     # generates number from 0 to b-1, incremented by 1
  3. range(a,b,c)  # generates numbers from a to b-1, incremented by c

conclusion

In this post we saw how to use the built-in range() function, how to convert a range to list or set or plug it in a for-loop and iterate over its values.

Share it!

Facebook
Twitter
LinkedIn
Reddit
Picture of Ellion

Ellion

Professional IT consultant, writer, programmer enthusiast interested in all sorts of coding.
Eats all cookies 🍪

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

Strictly Necessary Cookies

Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.

Google Analytics Cookies

This website uses Google Analytics to collect anonymous information such as the number of visitors to the site, and the most popular pages.

Keeping this cookie enabled helps us to improve our website.

HotJar Cookies

We use Hotjar in order to better understand our users’ needs and to optimize this service and experience. Hotjar is a technology service that helps us better understand our users’ experience (e.g. how much time they spend on which pages, which links they choose to click, what users do and don’t like, etc.) and this enables us to build and maintain our service with user feedback. Hotjar uses cookies and other technologies to collect data on our users’ behavior and their devices. This includes a device's IP address (processed during your session and stored in a de-identified form), device screen size, device type (unique device identifiers), browser information, geographic location (country only), and the preferred language used to display our website. Hotjar stores this information on our behalf in a pseudonymized user profile. Hotjar is contractually forbidden to sell any of the data collected on our behalf.

For further details, please see the ‘about Hotjar’ section of Hotjar’s support site.