How to create an array in Python?

WBOY
Release: 2023-09-21 13:25:02
forward
1485 people have browsed it

How to create an array in Python?

Arrays in Python are ndarray objects. To create arrays in Python, use the Numpy library. An array is a container that can hold a fixed number of elements, and these elements should be of the same type. To use arrays in Python, import the NumPy library.

First, let’s install the Numpy library -

pip install numpy
Copy after login

Import the required Numpy libraries -

import numpy as np
Copy after login

Create array

Example

Now let's create an array. Basic Numpy arrays are created using the array() function in NumPy -

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

Output

Array =  [ 5 10 15 20 25]
Copy after login

Create a two-dimensional array

Example

We will create a two-dimensional array, 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)
Copy after login

Output

Array =  [[ 5 10 15]
         [20 25 30]]
Copy after login

Get array dimensions

Example

To get array dimensions in Python, use numpy.ndim. For a one-dimensional array, the dimension is 1.

Similarly, for a 2D array, the dimensions will be 2, etc. Now let's look at an 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)
Copy after login

Output

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

Get the shape of the array

Example

The number of elements in each dimension of an array is called its shape. Use numpy.shape to get the array shape. Let's see an example of getting 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)
Copy after login

Output

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

Initialize array with zeros

Example

We can 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)
Copy after login

Output

Array = 
[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]
Array Shape =  (3, 3)
Copy after login

Sort the array

Example

To sort an array 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))
Copy after login

Output

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

The above is the detailed content of How to create an array in Python?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template