NUMPY BASIC EXERCISE 8


Check if values of an array in Numpy are complex, real or scalar 

In this exercise, we are going to check whether the values inside the array are real, complex or scalar.

Code

import numpy as np
arr = np.array([1, 0, 4j, 2, 3.1, 5+2j])
print("Initial array")
print(arr)

print("Checks the complex values :")
print(np.iscomplex(arr))

print("Checks the real values :")
print(np.isreal(arr))

print("Checks the scalar values :")
print(np.isscalar(arr))

Output

Initial array
[1. +0.j 0. +0.j 0. +4.j 2. +0.j 3.1+0.j 5. +2.j]


Checks the complex values :
[False False  True False False  True]


Checks the real values :
[ True  True False  True  True False]


Checks the scalar values :
False

Code Editor

import numpy as np arr = np.array([1, 0, 4j, 2, 3.1, 5+2j]) print("Initial array") print(arr) print("Checks the complex values :") print(np.iscomplex(arr)) print("Checks the real values :") print(np.isreal(arr)) print("Checks the scalar values :") print(np.isscalar(arr))