Python: Introduction to Numpy and To create several types of Array

What is Numpy?





Numpy is a module that contains several classes, functions or variables, etc. To do with scientific calculations in Python. Numpy is useful to create and also process single and multidimensional arrays.

An array is an object that store a group of elements of same data type it means that in case of numbers we can store only integer or only float but not one integer and one is a float. If you want to work with Numpy we need to import the Numpy module.

Example 1:

import numpy
arr=numpy.array([1,2,3,4,5,6,7])
print(arr)
output: 
array([1,2,3,4,5,6,7])

Example 2:

import numpy np
arr=np.array([1,2,3,4,5,6,7])
print(arr)
output: 
array([1,2,3,4,5,6,7])

Example 3:

from numpy import*
arr=array([1,2,3,4,5,6,7])
print(arr)

output: 
array([1,2,3,4,5,6,7])

Creating an array can be done in several always:

1.Using array()function

2.Using arrange()function

3.Using Zeroes() and once() function

1.Using array()function:

>>arr=array([1,2,3,4,5,6,7],int)

>>type(arr)

<class 'numpy.nparray'>

>>arr

array([1,2,3,4,5,6,7]

>>arr=array([1.5,2.4,3.7,4.3,5.8,6.9,7.1],int)

>>arr

array([1,2,3,4,5,6,7])

>>arr=array([1.5,2.4,3.7,4.3,5.8,6.9,7.1],float)

>>arr

array([1.5,2.4,3.7,4.3,5.8,6.9,7.1])

For String no need to specify data type

>arr=array(['a','b','c'])

>>print(arr)

['a' 'b' 'c']

2.Using arrange()function:

arrange(start,stop,steppoint)
>>from numpy import*
>>a=arrange(2,11,2)
>>>a
array([2,4,6,8,10])

3. Creating an array using zeros() and ones() functions:

zeros(n, datatype) --> create array with all zeros
ones(n,datatype)--> create array with all 1's
>> from numpy import*
>> a=zeros(5, int)
>>a
array([0,0,0,0,0])
>> b= ones(5,int)
>>b
array([1,1,1,1,1])

Mathematical Operations on arrays:

>>arr = array([1,2,3,4,5])
>>arr1=arr+5
>>arr1
array([6,7,8,9,10])