Python NumPy

Python Tutorial @ Home
Python Tutorial
Advance Python Tutorial

Python NumPy

What is Python NumPy
How to install Python NumPy
create a NumPy array
Add elements to an NumPy Array

Python Numpy 3

size:
Helps us to find total elements in the array

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

Output:

[1 2 3]
3

Example:

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

Check if NumPy array is empty

We can use the size method which returns the total number of elements in the array.
In the following example, we have an if statement that checks if there are elements in the array by using ndarray.size where ndarray is any given NumPy array:

import numpy as np
a=np.array([1, 2, 3])
if(a.size == 0):
    print("The given Array is empty")
else:
    print("The array = ", a)

In the above code, there are three elements, so it’s not empty and the condition will return false.

The array =  [1 2 3]

If there are no elements, the if condition will become true and it will print the empty message.
If our array is equal to:
a = np.array([])
The output of the above code will be as below:

import numpy as np
a=np.array([])
if(a.size == 0):
    print("The given Array is empty")
else:
    print("The array = ", a)

The given Array is empty