Numpy Checks it is a Scalar or an Array (Examples)

You can use the np.isscalar() to check whether a variable is a scalar or array. The following shows Python code examples checking it is a scalar or array.

Example 1: A number

np.isscalar(4.4)

Output:

True

Example 2: An array with one number

np.isscalar(np.array(4.4))

Output:

False

Since it is not a scalar, we can check whether it is an array. The following code checks whether it is an array in Python.

a=np.array(4.4)
type(a)

Output:

numpy.ndarray

Example 3: A list with one number

np.isscalar([4.4])

Output:

False

Since it is not a scalar, let’s check what it is. In the following, we can see it is a list.

a=[4.4]
type(a)

Output:

list

Example 4: a Boolean

We can also check whether a Boolean is considered as a scalar. We can see that it is.

np.isscalar(False)

Output:

True

Example 5: a word

We can also check whether a word can be considered as a scalar. We can see that it is.

np.isscalar('Hello')

Output:

True

Further Reading