
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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]]
- Related Articles
- How do you use Python to make websites?
- How do you make a higher order function in Python?
- How do you empty an array in C#?
- How do you convert an ArrayList to an array in Java?
- How do you limit an array sub-element in MongoDB?
- How do you make code reusable in C#?
- How do you perform an AND query on an array in MongoDB?
- How do you sort an array in C# in ascending order?
- How do you sort an array in C# in descending order?
- How do you find the length of an array in C#?
- How do you print the content of an array in Java?
- How do you make a list iterator in Java?
- How do I make an executable from a Python script?
- How do you check if a variable is an array in JavaScript?
- How do you convert a list collection into an array in C#?
