# Numpy Arrays | Creation, Update, Delete, Slicing, Append, Indexing etc

Numpy arrays can easily create with array function.  You can assign integer, float, complex data type values.

Syntax:

numpy.array(object, dtype=None, copy=True, order=’K’, subok=False, ndmin=0)
Create an array.

Parameters:
object : array_like
An array, any object exposing the array interface, an object whose __array__ method returns an array, or any (nested) sequence.

dtype : data-type, optional
The desired data-type for the array. If not given, then the type will be determined as the minimum type required to hold the objects in the sequence. This argument can only be used to ‘upcast’ the array. For downcasting, use the .astype(t) method.

copy : bool, optional
If true (default), then the object is copied. Otherwise, a copy will only be made if __array__ returns a copy, if obj is a nested sequence, or if a copy is needed to satisfy any of the other requirements (dtype, order, etc.).

order : {‘K’, ‘A’, ‘C’, ‘F’}, optional
Specify the memory layout of the array. If object is not an array, the newly created array will be in C order (row major) unless ‘F’ is specified, in which case it will be in Fortran order (column major). If object is an array the following holds.

order no copy copy=True
‘K’ unchanged F & C order preserved, otherwise most similar order
‘A’ unchanged F order if input is F and not C, otherwise C order
‘C’ C order C order
‘F’ F order F order
When copy=False and a copy is made for other reasons, the result is the same as if copy=True, with some exceptions for A, see the Notes section. The default order is ‘K’.

subok : bool, optional
If True, then sub-classes will be passed-through, otherwise the returned array will be forced to be a base-class array (default).

ndmin : int, optional
Specifies the minimum number of dimensions that the resulting array should have. Ones will be pre-pended to the shape as needed to meet this requirement.

Returns:
out : ndarray
An array object satisfying the specified requirements.

Example :

### Creation of Numpy Integer Array:

` ` ``` import numpy as np a= np.array([1,2,3]) print(a) print(a.dtype) ``` ` ` ` `

Output:

[1 2 3]

int32

### Creation of Numpy Float Array:

` ` ``` a= np.array([1.5,2.3,3.4]) print(a) print(a.dtype) ``` ` ` ` `

Output:

[1.5 2.3 3.4]

float64

### Creation of Complex Array:

` ` ``` a= np.array([1,2,3], dtype = complex) print(a) print(a.dtype) ``` ` ` ` `

[1.+0.j 2.+0.j 3.+0.j]

complex128

### How to Create Numpy Two Dimensional arrays

Two Dimension arrays: we can create two dimensional array with the data inside braced  [(),()]

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

Output:

[[1 2 3]

[4 5 6]]

2

### Array Size:

You can get know the size of the array using variable.size . array.size will help to know the number of elements in array.

Example:

` ` ``` import numpy as np a= np.array([(1,2,3),(4,5,6),(7,8,9),(1,2,3),(4,5,6),(7,8,9)]) print(a.size) ``` ` ` ` `

Output:

18

### numpy.zeros() function:

This function helps us to create zeros array with desired dimension.

Example program

` ` ``` #creation of numpy array with zeros import numpy as np #single dimention a= np.zeros(10) print('\n one dimensional array',a) #two dimensional zero array a= np.zeros((2,2)) print('\nprinting two dimensional array',a) #three dimensional zero array a= np.zeros((3,3,3)) print('\nprinting three dimensional array',a) ``` ` ` ` `

### numpy.ones() function:

This function helps us to create ones array with desired dimension.

Example program

` ` ``` #creation of numpy array with ones import numpy as np #single dimension a= np.ones(10) print('\n one dimensional array',a) #two dimensional ones array a= np.ones((2,2)) print('\n printing two dimensional array',a) #three dimensional ones array a= np.ones((3,3,3)) print('\n printing three dimensional array',a) ``` ` ` ` `

## Numpy Array Operations:

Arithmetic Operations in Numpy Arrays. You can do add, multiply, subtract, division of array using numpy.

` ` ``` import numpy as np a= np.array([1,2,3]) b= np.array([3,5,6]) c=a+b print(c) ``` ` ` ` `

Output:

[4 7 9]

### Numpy Arrays Subtract:

` ` ``` import numpy as np a= np.array([1,2,3]) b= np.array([3,5,6]) c= b-a print(c) ``` ` ` ` `

Output:

[-2 -3 -3]

### Numpy Arrays Multiply:

` ` ``` import numpy as np a= np.array([1,2,3]) b= np.array([3,5,6]) c=a*b d=a*2 print(c) print(d) ``` ` ` ` `

Output:

[ 3 10 18]

[2  4  6]

### Numpy Arrays Division:

` ` ``` import numpy as np a= np.array([1,2,3]) b= np.array([3,5,6]) c=a/b print(c) ``` ` ` ` `

Output:

[0.33333333 0.4        0.5       ]

### Indexing of Numpy Arrays:

the numpy array index also same like arrays, start with zero. you can element by index number.

` ` ``` import numpy as np a= np.array([1,2,3]) print(a[0]) print(a[1]) ``` ` ` ` `

### Negitive Index of Numpy Arrays:

Negitive index helps to call the array elements from last index.

` ` ``` import numpy as np a= np.array([1,2,3]) print(a[-1]) print(a[-2]) ``` ` ` ` `

Slicing of Numpy Arrays:

In numpy we use slicing operation [] to slice the array elements.  if you want particular elements from 2 to 5 index range, we have use slicing [2:5] like that.

systax: arrayname[start, end, step]

#example program on numpy array slicing on single dimension

` ` ``` import numpy as np a= np.array([1,2,3,4,5]) print(a[2:4]) ``` ` ` ` `

#example program on numpy array slicing on two dimension

` ` ``` import numpy as np a= np.array([(1,2,3,4,5),(6,7,8,9,10), (11,12,13,14,15)]) print(a[:2, :3]) # two rows, three columns print(a[1:, ::3])# second row, two columns ``` ` ` ` `

### How to access first column of numpy arrays

` ` ``` import numpy as np a= np.array([(1,2,3,4,5),(6,7,8,9,10), (11,12,13,14,15)]) print(a[:, 0]) ``` ` ` ` `

### How to access first row of numpy arrays

` ` ``` import numpy as np a= np.array([(1,2,3,4,5),(6,7,8,9,10), (11,12,13,14,15)]) print(a[0,:]) ``` ` ` ` `

### How to Create a copy of numpy arrays:

Copy of numpy arrays done by using numpy.copy() function.

` ` ``` import numpy as np a= np.array([(1,2,3,4,5),(6,7,8,9,10), (11,12,13,14,15)]) b=np.copy(a) print(b) ``` ` ` ` `

### How to Create a reshape numpy arrays:

We can do reshaping of arrays by using numpy.reshape() function.

` ` ``` import numpy as np a= np.array([(1,2,3,4,5),(6,7,8,9,10), (11,12,13,14,15)]) b=np.reshape(a,(1,15)) print(b) c=np.reshape(a,(5,3)) print(c) ``` ` ` ` `

How to Transpose numpy arrays:

We can easily transpose numpy arrays using numpy.T function()

` ` ``` import numpy as np a= np.array([(1,2,3,4,5),(6,7,8,9,10), (11,12,13,14,15)]) b=a.T print(a) print(b) ``` ` ` ` `

How to Concatenate Numpy Arrays:

How to Compare Arrays

How to Merger Numpy arrays:

How to Split Numpy Arrays :

We can split numpy arrays by using split, hsplit, vsplit funtions.

How to Generate Random numbers using Numpy arrays :

How to Do Sorting using Numpy arrays: