Python: range(), numpy.arange(), and numpy.linspace()

Rahul S
3 min readSep 9, 2023

1. range()

  • range() is a built-in Python function used to create a sequence of integers from a starting value (inclusive) to an ending value (exclusive).
range([start], stop, [step])
#Generating a simple sequence of numbers:

for i in range(5):
print(i)
# Output: 0 1 2 3 4
#Specifying start, stop, and step values:

for i in range(1, 10, 2):
print(i)
# Output: 1 3 5 7 9
# Creating a list from a range:

my_list = list(range(3, 15, 3))
# my_list will be [3, 6, 9, 12]

2. numpy.arange()

  • numpy.arange() is a function from the NumPy library used to create arrays with evenly spaced values within a specified range.
  • It allows specifying the start, stop, and step values.
numpy.arange([start], stop, [step], dtype=None)
#Creating an array with arange:

import numpy as np
arr = np.arange(0, 10, 2)
# arr will be array([0, 2, 4, 6, 8])
#Specifying a float data type:

import numpy as np
arr = np.arange(0, 1, 0.1, dtype=float)

# arr will be array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])
# Using arange for creating a sequence of dates:

import numpy as np…

--

--