Jump to content

Python Generate Sequence of Numbers

Recommended Posts

Python language can be used for various tasks, such as data processing, web development, ML, AI, and more. Python can also be utilized to generate/create sequences of numbers, such as ranges, lists, arrays, or iterators. This blog presents a detailed tutorial on generating a sequence of numbers in Python via the below methods:

How to Generate Sequences of Numbers in Python?

Method 1: Generate Sequence of Numbers Using Python “range()” Function

The “range()” function in Python is employed to create/generate a number sequence by taking the start, stop, and step values as an argument.


range(start, stop[, step])

In the above syntax:

  • The “start”, “stop”, and “step” parameters specify the sequence start value, end value, and the gap between each sequence number.

Example 1: Generate a Sequence of Numbers

The “for” loop is utilized along with the “range()” function to generate/construct the sequence and display them to the console. Take a look at the below code:

for item in range(5):

The above code execution generates the following sequence of numbers:


Example 2: Generate a Sequence of Numbers Within a Specific Range

The “range()” function takes the start and end values as an argument to generate/construct the number sequence within the defined/specified range.

for item in range(8,15):

As a result of executing the above code, the following sequence of numbers is generated:


Method 2: Generate a Sequence of Numbers Using “List Comprehension”

The “List Comprehension” is used to iterate over the specified range and generate a sequence of specified numbers. Here is an example code:

list_comp = [item for item in range(2, 15)]

The execution of the above code generates the below output:


Method 3: Generate a Sequence of Numbers Using “itertools”

The “itertools” module provides various tools for creating iterators and working with sequences. The “itertools.chain()” function of the “itertools” module takes the “range()” function as an argument and generates the sequence of numbers. Here is an example code:

import itertools
print(list(itertools.chain(range(5, 12))))

This code generates the sequence of numbers, as shown in the below snippet:


That’s all about generating a sequence of numbers in Python.


To generate a sequence of numbers, various methods, such as the “range()” function, “List Comprehension”, and “itertools”, are used in Python. The “range()” function is used along with the “for” loop to generate the sequence with a specified range. The “List Comprehension” and “itertools” methods can be utilized to generate/create number sequences. This Python guide presented a detailed guide on generating sequences of numbers in Python using numerous examples.

View the full article

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...