Numpy
Numpy
NumPy, which stands for Numerical Python, is a powerful library in Python used for numerical
computing. It is a general-purpose array-processing package. NumPy provides the ndarray (N-
dimensional array) data structure, which represents arrays of any dimension. These arrays are
homogeneous (all elements are of the same data type) and can contain elements of various
numerical types (integers, floats, etc.)
Creating a Numpy Array - Arrays in Numpy can be created by multiple ways. Some of the
ways are programmed here:
1) 1 D Array:
(i) Using List
import numpy
arr=numpy.array([10,20,30])
print (arr) output: [10 20 30]
2) 2 D Array:
(iii) Using List
import numpy
arr=numpy.array([[10,20,30],[34,56,78]])
print (arr) output: [[10 20 30]
[34 56 78]]
Using values from the user (using empty( )-- The empty() function in Python is used to
return a new array of a given size)
Output:
import numpy as np enter the size of the array:5
n=int(input("enter the size of the array:")) enter the number:11
enter the number:12
arr=np.empty(n) enter the number:13
for i in range(n): enter the number:14
arr[i]=int(input("enter the number:")) enter the number:15
Array:
print ("Array:\n", arr) [11. 12. 13. 14. 15.]
Using np.ones() to create an ND Array with Ones:
import numpy as np
arr=np.ones(5)
print(arr) Output: [1. 1. 1. 1. 1.]
import numpy as np
arr=np.zeros(5)
print(arr) Output: [0. 0. 0. 0. 0.]
import numpy as np
arr=np.arange(0,5,2)
print(arr) Output: [0 2 4]