Python NumPy Array + Examples

In this Python NumPy tutorial, you will learn about python numpy array, how to create an array using Python NumPy and, also we will check:

  • Numpy array creation
  • Numpy.empty method
  • Numpy.zeros method
  • Numpy.ones methods
  • Numpy.reshape method
  • Python Numpy array example
  • Python numpy array size
  • Create Numpy ndarray object
  • What is Array Dimension
  • 0-D arrays in Numpy
  • 1-D arrays in Numpy
  • 2-D arrays in Numpy
  • 3-D arrays in Numpy
  • What’s the difference between the Numpy array and the python list?

How to create Numpy array

NumPy offers many functions for creating arrays in Python with initial placeholder content. Also, it minimizes the necessity of growing arrays, which is an expensive operation. For example NumPy.empty, NumPy.zeros methods we can use to create arrays in Python using NumPy, etc.

Numpy.empty method

Let’s see Numpy.empty method to create an array in Python.

The np.empty() method is used to create an uninitialized array of the specified arrays of specified shapes and data types. It contains junk values.

import numpy as np  
my_arr = np.empty((2,2), dtype = int)  
print(my_arr)  

You can refer to the below screenshot to see the output for Numpy.empty methods.

Numpy.empty methods
Numpy.empty methods

This is how to create an uninitialized array in Python using NumPy.

Read: Python program to print element in an array

Numpy.zeros method

Let us see Numpy.zeros methods in Python NumPy to create an array.

The numpy.zeros() is used to create the NumPy array with the specified shape where each NumPy array item is initialized to 0.

import numpy as np  
my_arr = np.zeros((3,3), dtype = int)  
print(my_arr) 

You can refer to the below screenshot to see the output for Numpy.zeros methods.

What is Numpy zeros in Python
Numpy.zeros method

This is how we can use the Python Numpy.zeros method to create an array.

Read: Python NumPy Random + Examples

Numpy.ones method

Now, we will see Numpy.ones method to create a NumPy arrary in Python.

The np.ones() is used to create the NumPy array with the specified shape where each NumPy array item is initialized to 1.

import numpy as np  
my_arr = np.ones((3,3), dtype = int)  
print(my_arr) 

You can refer to the below screenshot to see the output for Numpy.ones methods

numpy ones function
Numpy.ones methods

This is how to create a NumPy array with the specified shape in Python.

Read: Python concatenate arrays

NumPy.reshape method

Let us see, how to use NumPy.reshape method in Python.

The numPy.reshape() method is used to shape an array without changing data of array. The shape array with 2 rows and 3 columns.

import numpy as np  
my_arr = np.arange(6).reshape(2, 3)
print("\nArray reshaped with 2 rows and 3 columns : \n", my_arr)

You can refer to the below screenshot to see the output for NumPy.reshape methods

NumPy.reshape method
NumPy.reshape method

This is how we can work with, NumPy.reshape method in Python.

These are various python numpy array functions.

Python NumPy array example

Let’s see Python NumPy array example.

  • Firstly we need to import NumPy as np.
  • Then we have declared the variable as my_arr = np.array([101, 102, 103, 104, 105])
  • At last to get the output print(my_arr).

Example:

import numpy as np
my_arr = np.array([101, 102, 103, 104, 105])
print(my_arr)

You can refer to the below screenshot to see the output for Python NumPy array example.

Python NumPy array example
Python NumPy array example

Here, we saw a simple example of Python NumPy array.

Read: How to write Python array to CSV

Python numpy array size

Here, we will see Python numpy array size.

To get the size (number of all elements) in numpy array we will use size attributes to get the array size in the output.

Example:

import numpy as np
my_arr = np.array([0,1,2], dtype=np.float64)
print("Size of the array: ", my_arr.size)

You can refer to the below screenshot to see the output for python numpy array size

Python numpy array size
Python numpy array size

This is how to get the Python NumPy array size.

Create NumPy ndarray object

Now, we will see how to create NumPy ndarray object in Python.

Numpy is used to work with array, the array object in numpy is called ndarray.

import numpy as np
my_arr = np.array([5,6,7,8,9])
print(my_arr)
print(type(my_arr))

You can refer to the below screenshot to see the output for Create NumPy ndarray object

Create NumPy ndarray object
Create NumPy ndarray object

This is how to work with NumPy ndarray in Python.

Read: Python shape of an array

Python NumPy array dimensions

A dimension is a direction in which you can vary the specification of an array’s elements. Dimension in an array is one level of array depth. Let us see a few examples of python numpy array dimensions.

0-D arrays in Numpy

Lets us see how to create a 0-D arrays in Numpy.

  • The 0-D arrays in Numpy are scalar and they cannot be accessed via indexing.
  • Firstly we will import numpy as np.
  • The 0-D arrays are the elements in an array. Also, each value in an array is a 0-D array.
import numpy as np
my_arr = np.array(50)
print(my_arr)

You can refer to the below screenshot to see the output for 0-D arrays in Numpy.

0-D arrays in Numpy
0-D arrays in Numpy

This is how to work with 0-D arrays in Numpy python.

1-D arrays in Numpy

Now, we will see 1-D arrays in Numpy python.

  • 1-D arrays in numpy are one dimension that can be thought of a list where you can access the elements with the help of indexing.
  • Firstly we will import numpy as np.
  • The array which has 0-D arrays as its elements is called 1-D arrays.
import numpy as np
my_arr = np.array([10, 11, 12, 13, 14])
print(my_arr)

You can refer to the below screenshot to see the output for 1-D arrays in Numpy.

1-D arrays in Numpy
1-D arrays in Numpy

This is how to work with 1-D arrays in Numpy in python

2-D arrays in Numpy

Now, we will see how to create a 2-D arrays in Numpy in Python.

  • 2-D arrays in numpy are two dimensions array that can be distinguished based on the number of square brackets used.
  • Firstly we will import numpy as np.
  • The array which has 1-D arrays as its elements is called 2-D arrays.
import numpy as np
my_arr = np.array([[11, 12, 13], [14, 15, 16]])
print(my_arr)

You can refer to the below screenshot to see the output for 2-D arrays in Numpy.

2-D arrays in Numpy
2-D arrays in Numpy

This is how to work with 2-D arrays in Numpy python.

Read: Python Array with Examples

3-D arrays in Numpy

Here, you will see 3-D arrays in Numpy python

  • The 3-D arrays in numpy are the three-dimension array that can have three square brackets with the numpy array.
  • Firstly we will import numpy as np.
  • The array which has 2-D arrays as its elements is called 3-D arrays.
import numpy as np
my_arr = np.array([[[11, 12, 13], [14, 15, 16]], [[11, 12, 13], [14, 15, 16]]])
print(my_arr)

You can refer to the below screenshot to see the output for 3-D arrays in Numpy

3-D arrays in Numpy
3-D arrays in Numpy

This is how to work with 3-D arrays in Numpy python.

Difference between NumPy array and Python list

NumPy ArrayPython List
Numpy arrays are faster and more compact.Python lists are not much faster and compact.
Can store only one data type in an array at any time.Able to store different data types in the same list.
NumPy gives an enormous range of fast and efficient ways of creating arrays and manipulating numerical data inside them.While python lists can contain different data types within a single list.
NumPy array vs Python list or python numpy array vs list

You may like the following Python tutorials:

In this python numpy array tutorial, we learned about Python NumPy Array and also we have seen how to use it like:

  • Numpy array creation
  • Numpy.empty method
  • Numpy.zeros method
  • Numpy.ones method
  • Numpy.reshape method
  • Python Numpy array example
  • Python numpy array size
  • Create Numpy ndarray object
  • What is Array Dimension in python
  • Python 0-D arrays in Numpy
  • Python 1-D arrays in Numpy
  • Python 2-D arrays in Numpy
  • Python 3-D arrays in Numpy
  • Difference between Numpy array and the python list