Python Numpy Array | How To Create A Numpy Array

Chapter: Python Numpy Last Updated: 09-07-2021 09:59:00 UTC

Program:

            /* ............... START ............... */
                import numpy as np

arr = np.array([1, 2, 3, 4, 5])

print(arr)

print(type(arr))

/* Output


[1 2 3 4 5]
<class 'numpy.ndarray'>

*/

import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])

print(arr)

/ * Output
[[1 2 3]
 [4 5 6]]

*/

import numpy as np

one = np.array(45)
two = np.array([1, 2, 6, 4, 5])
three = np.array([[1, 2, 3], [4, 5, 6]])
four = np.array([[[1, 2, 3], [4, 6, 6]], [[1, 9, 3], [6, 8, 6]]])

print(one.ndim)
print(two.ndim)
print(three.ndim)
print(four.ndim)


/*Output
0
1
2
3
*/

                /* ............... END ............... */
        

Notes:

  • Numpy is the python library used to create array. We can create a NumPy ndarray object by using the array() function.
  • type() is function in python used to give the type of object. In the above program you can see that array is ndarray.
  • By using numpy you can also create a two diamentional array also. These are often used to represent matrix or 2nd order tensors.
  • ndim attribute in Numpy is used to know the dimensions of the array.
Similar Programs Chapter Last Updated