Python arrays using numpy

numpy is a package that contains several classes, functions, variables etc. to deal with scientific calculations in Python. numpy is useful to create and also process single and multi-dimensional arrays. In addition, numpy contains a large library of mathematical functions like linear algebra functions and Fourier transforms.

To work with numpy, we should first import numpy module into our Python programs as:

import numpy

we can use any of the objects from that package. But, to refer to an object we should use the format:numpy.object .

Installing numpy

pip install numpy

Python arrays using numpy

Python program to create a simple array using numpy.

import numpy
arr = numpy.array([10, 20, 30, 40, 50]) 

Second version of Program to create an array.

import numpy as np 
arr = np.array([10, 20, 30, 40, 50]) 

Third version of Program to create an array.

from numpy import * 
arr = array([10, 20, 30, 40, 50]) 

Creating arrays in numpy can be done in several ways. Some of the important ways are:

  1. Using array() function
  2. Using linspace() function
  3. Using logspace() function
  4. Using arange() function
  5. Using zeros() and ones() functions