Create a sequence of numbers in python

create_sequence_of_numbers

Skill - Create a sequence of numbers in python

Table of Contents

Skills Required

Please make sure to have all the skills mentioned above to understand and execute the code mentioned below. Go through the above skills if necessary for reference or revision


In this post we will learn how to create sequence of numbers in python using three approaches

  • using range function with syntax range(start, stop, step)
  • using list comprehensions (which are one basically liners of a for loop)
  • using arrange and linspace functions of numpy

These sequences of number can be used in for loops and many other situations

Approach 1 - using range function

‘range’ is a python built-in sequence generator function through which we can generate sequences

Syntax

range(start, stop, step)

The post on how to use range function can be seen here

‘range’ function code example

# create a sequence from 0 to 4, i.e., 0,1,2,3,4
x = range(5)

# create a sequence from 1 to 7, i.e., 1,2,3,4,5,6,7
x = range(1,8)

# sequence from 2 to 12 with steps of 2, i.e., 2,4,6,8,10,12
x = range(2,13,2)

# create a list from range using the * operator inside square brackets
y = [*x]
# Now y is a list which is derived from a range x

Approach 2 - using list comprehensions

List Comprehensions can be used to create a list from another list/sequence in a user-friendly way and with less lines of code.
The post on how to use list comprehensions can be seen here

list comprehension code example

lst = [t for t in range(15)]

print(lst)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

# using filter in list comprehension to create a sequence of even numbers
lst = [x for x in range(0,17) if x%2==0]

print(lst)
# [0, 2, 4, 6, 8, 10, 12, 14, 16]

Approach 3 - using numpy ‘arange’ and ‘linspace’ functions

This approach is very useful if you want to create sequence of decimal numbers
Make sure you have numpy module installed by typing the following in the command line

pip install numpy

numpy arange code example

Use arange function for creating a sequence with fixed interval between numbers (also called ‘step’)
The syntax is np.arange(start, stop, step) , start and step are optional

# import the numpy module
import numpy as np

# only 'stop' input given to arange function
s = np.arange(5).tolist()

print(s)
# [0, 1, 2, 3, 4]

# 'start' and 'stop' input given to arange function
s = np.arange(1,6).tolist()
print(s)
# [1, 2, 3, 4, 5]

# 'start', 'stop', 'step' input given to arange function
s = np.arange(1,6, 0.5).tolist()
print(s)
# [1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5]

numpy linspace code example

Use linspace function for creating a sequence with fixed number of samples from start to end
The syntax is np.linspace(start, stop, num)

# import the numpy module
import numpy as np

# get 15 evenly spaced numbers from 1 to 8 using linspace function
s = np.linspace(1,8,num=15).tolist()

print(s)
# [1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0, 7.5, 8.0]

Video

Video for this post can be found here


Online Interpreter

You can run these codes online at https://www.programiz.com/python-programming/online-compiler/

References


Table of Contents

Comments