Return evenly spaced values within a given interval and step size in Numpy

NumpyServer Side ProgrammingProgramming

Create an array with int elements using the numpy.arange() method. The 1st parameter is the "start" i.e. the start of the interval. The 2nd parameter is the "end" i.e. the end of the interval. The 3rd parameter is the step size i.e. the spacing between values. The default step size is 2 here.

Values are generated within the half-open interval [start, stop). For integer arguments the function is equivalent to the Python built-in range function, but returns an ndarray rather than a list.

The stop is the end of interval. The interval does not include this value, except in some cases where step is not an integer and floating point round-off affects the length of out. The step is the spacing between values. For any output out, this is the distance between two adjacent values, out[i+1] - out[i]. The default step size is 1. If step is specified as a position argument, start must also be given.

Steps

At first, import the required library −

import numpy as np

Creating an array with int elements using the numpy.arange() method. We have set the step size to 2 here −

arr = np.arange(15, 30, step = 2)
print("Array...\n", arr)

Get the type of the array −

print("\nArray type...\n", arr.dtype)

Get the dimensions of the Array: −

print("\nArray Dimensions...\n",arr.ndim)

Get the shape of the array −

print("\nOur Array Shape...\n",arr.shape)

Get the number of elements of the Array −

print("\nNumber of elements in the Array...\n",arr.size)

Example

import numpy as np

# Creating an array with int elements using the numpy.arange() method
# The 1st parameter is the "start" i.e. the start of the interval
# The 2nd parameter is the "end" i.e. the end of the interval
# The 3rd parameter is step size i.e. the spacing between values. The default step size is 1
# We have set the step size to 2 here
arr = np.arange(15, 30, step = 2)
print("Array...\n", arr)

# Get the array type
print("\nArray type...\n", arr.dtype)

# Get the dimensions of the Array
print("\nArray Dimensions...\n",arr.ndim)

# Get the shape of the Array
print("\nOur Array Shape...\n",arr.shape)

# Get the number of elements of the Array
print("\nNumber of elements in the Array...\n",arr.size)

Output

Array...
[15 17 19 21 23 25 27 29]

Array type...
int64

Array Dimensions...
1

Our Array Shape...
(8,)

Number of elements in the Array...
8
raja
Updated on 10-Feb-2022 10:02:01

Advertisements