0% found this document useful (0 votes)
11 views2 pages

Numpy

Uploaded by

Pooja Bansal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

Numpy

Uploaded by

Pooja Bansal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

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.)

NumPy can be installed using Python's package manager, pip.


pip install numpy

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]

(ii) Using Tuple


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]]

(iv) Using Tuple


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.]

Using np.zeros() to create an ND Array with zeros:

import numpy as np
arr=np.zeros(5)
print(arr) Output: [0. 0. 0. 0. 0.]

Using np.arange() to create an ND Array:

import numpy as np
arr=np.arange(0,5,2)
print(arr) Output: [0 2 4]

You might also like