- The range() method in Python and how to use it
- How the parameters of range function() like “start”, “stop”, and “step” can be customized
- How to use Python’s range() function in reverse
- How to use range() with a for loop
- And a lot more
What Is Range() Function in Python
You can create a series of numbers using the “start”, “stop”, and “step” parameters in the Python range() function. The range behins at 0 by default, increase by 1, and ends just before the specified value. Let’s take a moment to note something before moving on. Range() in Python is a type rather than a function. A “range” type is returned when the range() object is instantiated. This has the advantage of using a bit less memory than the other container types like tuples or lists. The term “function” is used in this tutorial instead of “type” to keep things straightforward and useful.
Syntax: range(start, stop, step)
start: An integer. The start index has a default value of 0 if it is not specified. It is an optional parameter.
stop: The range function must stop at a certain value which is determined by the stop value. It is a mandatory parameter for the range function.
step: The final number is always one less than the stopping point. The increment value that needs to be made to the following number range, and by default, is 1. It is also an optional parameter.
Creating the Range() Object
Let’s examine how to generate a range of numbers with the help of the range() method:
print('numbers in range: ', numbers)
print('Type: ', type(numbers))
We entered a range of numbers in the previous script, ranging from 0 to 6 but not including 6. The given snippet includes two important points:
When a range() is printed, the object range itself is printed rather than the values and a class of the type range is the result of instantiating a range().
Printing Numbers in Range() Object
By iterating through each item/number in the object, the values can be printed. By setting the end parameter in the print() function, we can print each item on the same line. By specifying a string that contains just scape (“ “) in the end parameter, we can print the print items on the same line, separated by white spaces.
for n in numbers:
print(n, end=' ')
As can be seen, the function returned the numbers between 0 and 6.
Let’s look at how Python allows us to modify the range’s start parameter.
Generating a Sequence of Numbers by Specifying the Start Parameter in Range() Function
As previously stated, the Python range() function starts at 0 by default. However, by changing the start= value, you can change this value. Let’s look at how we can set our parameters to start the range at 10 and stop before 16.
for n in numbers:
print(n, end=' ')
We use a for loop to print the range/sequence of numbers which are generated using the range() function. We specify the start parameter as 10 and the stop as 16. The function prints the values from 10 to 15 and doesn’t include the number 16 because a sequence of numbers that are generated using the range() function doesn’t include the specified value for the stop parameter. The “start” parameter is inclusive, therefore the value is also included (unlike the “stop” parameter ). We will learn about the “step” parameter in the section below.
Generating a Sequence of Numbers by Specifying the Set Parameter in Range() Function
To define how the values are incremented or decremented, we can use the “step” parameter in the range() function. By default, this “set” parameter is set to 1 which means that the numbers ranging from start to stop are incremented by 1.
Let’s specify a value to set a parameter other than one to generate a sequence of numbers.
for value in values:
print(value, end=' ')
We set a range from 0 to 15 and increment it by 3 in the given code. This indicates that we generate a sequence of numbers that are multiples of 3, ranging from 0 to 15.
Now that you are aware of all the parameters that can be used with the range() type, let’s look at some usage, cases, and peculiarities of the object.
Using Range() in For Loop to Generate a Number Sequence
In many situations, we are required to repeat a task using the Python range() object. This can be simple to accomplish while using the object in our loop to access its range of numbers or items. The range function makes it simple to specify how many times a piece/block of code should execute. When range(6) is supplied as an example, the for loop cycles 6 times.
Generating a Reverse Number Sequence
Python makes it simple to create a range of numbers in reverse by allowing the “step” parameter to be set to a negative value. You can generate a sequence of numbers in reverse order by passing a negative step number to the range() object. Let’s create a range from 10 to 0 that decreases by 1.
print(num, end=' ')
The negative step can also be used to create a range of negative values. Let’s make a new range that goes from -5 to -15.
print(num, end=' ')
Creating a List Using Range() to Store the Sequence of Numbers
The numbers are stored in a memory-effective manner when you create them using a range object. Although, we might be requires to transform the ranges into Python lists, in some cases. Now, using the list() constructor function method makes this simple. To create a list of a specified range, we can just pass the range() inside the list() type.
As can be seen, the required list with a sequence of numbers is returned by the function.
Generating a Sequence of Float Numbers
The range() method only functions with integers by default. The program crashes and displays a TypeError if you pass it in floats.
A range object cannot be directly created with a float. Our range object can be transformed into a list, and then we can convert the items in the list to float.
f = 
for n in num:
To iterate over every element in the range, we employ a for loop. Then, we use the float() function to cast the object into a floating value and add it to a list using the append() function.
The list comprehension method can make this method simpler. When you need to create a new list, the list comprehension provides a more compact syntax.
float_sequence = [float(n) for n in num]
Generating a Sequence of Float Numbers Using NumPy
You can generate a sequence of float numbers using the NumPy.arange() function. Ranges that increment using the float values can be made using this technique. Let’s examine how to generate a range of floating numbers with an increment of 2.5 from 0.0 to 15.0:
f = np.arange(0.0, 15.0, 2.5)
We import the NumPy module. Then, we create a range of numbers from 0.0 to 15.0 that is incremented by 2.5 using the arange() function.
We tried to teach how to make the value sequences in Python with range() in this tutorial. Understanding the range function’s multiple parameters was the first step towards using it. After that, we discussed how to alter the function’s behavior using its parameters. We created a sequence of int numbers and list objects with a sequence of integers of a specified range. We also implemented some examples to create a sequence of numbers of the float datatype in Python.