NumPy: Create and Access elements in ndarray

In Python ,Numpy creates an array which is called as ndarray.  It is table of elements. It is the n-dimensional array.It is used in python because it is faster than list. It describes the collection of items of the same type.

Create Ndarray:

In numpy, we can create array by using array() function.

Syntax:

numpy.array()

0-D Array:

Example:

import numpy 
a = numpy.array(89)
print(a)

Output: 89

1-D Array:

Example:

import numpy 
a = numpy.array(["a","b","c"])
print(a)

Output: ['a' 'b' 'c']

2-D Array:

Example:

import numpy
a = numpy.array([[1,2,3],[4,5,6]])
print(a)

Output:

[[1 2 3]
 [4 5 6]]

3-D Array:

Example:

import numpy
a = numpy.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]])
print(a)

Output:

[[[ 1  2  3]
  [ 4  5  6]]

 [[ 7  8  9]
  [10 11 12]]]

Access Elements in Ndarray:

Each element of an array is having the same size in the memory. Elements in Numpy arrays are accessed by using square brackets and it can be accessed using a zero-based index.The elements of an ndarray can be accessed using by indexing or slicing the array.

Example:

import numpy

a = numpy.array([1, 2, 3])
print(a[0])
print(a[1])
print(a[2])

Output:

1 
2 
3