Python

Python Generate Sequence of Numbers

A sequence of numeric values between a specified range of values can be produced using the Python range() function. Using the helpful examples in this tutorial, you’ll learn everything about the range() method. Although the function appears to be quite simple to use, there are some functionalities that you might not be aware of. After finishing this tutorial, you will learn to know the following:

  • 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)

Where:

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:

Code:

numbers = range(6)
print('numbers in range: ', numbers)
print('Type: ', type(numbers))

Output:

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.

Code:

numbers = range(8)
for n in numbers:
    print(n, end=' ')

Output:

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.

Code:

numbers = range(10, 16)
for n in numbers:
    print(n, end=' ')

Output:

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.

Code:

values = range(0, 15, 3)
for value in values:
    print(value, end=' ')

Output:

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.

Code:

for num in range(6):
    print(num)

Output:

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.

Code:

for num in range(10, 0, -1):
    print(num, end=' ')

Output:

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.

Code:

for num in range(-5, -15, -1):
    print(num, end=' ')

Output:

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.

Code:

list_of_num = list(range(1,6))
print(list_of_num)

Output:

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.

Code:

num = range(5.0)

Output:

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.

Code:

num = range(5)
f = []
for n in num:
    f.append(float(n))
print(f)

Output:

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.

Code:

num = range(5)
float_sequence = [float(n) for n in num]
print(float_sequence)

Output:

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:

Code:

import numpy as np
f = np.arange(0.0, 15.0, 2.5)
print(f)

Output:

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.

Conclusion

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.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.