# How do you make an array in Python?

The arrays in Python are ndarray objects. To create arrays in Python, use the Numpy library. Array is a container which can hold a fix number of items and these items should be of the same type. To work with arrays in Python, import the NumPy library.

First, let us first install the Numpy library âˆ’

pip install numpy


Import the required Numpy library âˆ’

import numpy as np


## Create an Array

### Example

Let us now create an array.. The basic Numpy Array is created using an array() function in NumPy âˆ’

import numpy as np
# Create a Numpy Array
arr = np.array([5, 10, 15, 20, 25])
print("Array = ",arr)


### Output

Array =  [ 5 10 15 20 25]


## Create a Two-Dimensional Array

### Example

We will create a 2D array i.e. a matrix. Here, a 2x3 matrix will be created âˆ’

import numpy as np

# Create a Numpy Matrix 2x3
a = np.array([[5, 10, 15], [20, 25, 30]])

# Display the array with more than one dimension
print("Array = ",a)


### Output

Array =  [[ 5 10 15]
[20 25 30]]


## Get the Array Dimensions

### Example

To get the array dimensions in Python, use the numpy.ndim. For a 1D array, the dimensions are 1.

Similarly, for a 2D array, the dimensions will be 2, etc. Let us now see the example âˆ’

import numpy as np

# Create a Numpy Matrix 2x3
arr = np.array([[5, 10, 15], [20, 25, 30]])

# Display the array with more than one dimension
print("Array = \n",arr)
print("Array Dimensions = ",arr.ndim)


### Output

Array =
[[ 5 10 15]
[20 25 30]]
Array Dimensions =  2


## Get the Shape of an Array

### Example

The count of elements in each dimension of an array is called the shape. Use the numpy.shape to get the array shape. Let us see an example to get the shape of an array âˆ’

import numpy as np

# Create a Numpy Matrix 2x3
arr = np.array([[5, 10, 15], [20, 25, 30]])

# Display the array
print("Array = \n",arr)
print("Array Shape = ",arr.shape)


### Output

Array =
[[ 5 10 15]
[20 25 30]]
Array Shape =  (2, 3)


## Initialize Array with Zeros

### Example

We an easily initialize Numpy arrays with zeros âˆ’

import numpy as np

# Create a Numpy Matrix 3x3 with zeros
arr = np.zeros([3, 3])

# Display the array
print("Array = \n",arr)
print("Array Shape = ",arr.shape)


### Output

Array =
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]
Array Shape =  (3, 3)


## Sort Arrays

### Example

To sort arrays in Numpy, use the sort() method âˆ’

import numpy as np

# Create a Numpy Matrix
arr = np.array([[5, 3, 8], [17, 25, 12]])

# Display the array
print("Array = \n",arr)

# Sort the array
print("\nSorted array = \n", np.sort(arr))


### Output

Array =
[[ 5  3  8]
[17 25 12]]
Sorted array =
[[ 3  5  8]
[12 17 25]]