VOOZH about

URL: https://www.geeksforgeeks.org/python/numpy-isscalar-python/

⇱ numpy.isscalar() in Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

numpy.isscalar() in Python

Last Updated : 2 Jan, 2024

In this article, we will elucidate the `numpy.isscalar()` function through a well-documented code example and comprehensive explanation.

Python numpy.isscalar() Syntax

Syntax : numpy.isscalar(element)

Parameters:

  • element: The input element to be checked for scalar properties.

Return Type:

  • bool: Returns True if the input element is a scalar, and False otherwise.

What is numpy.isscalar() in Python?

The `numpy.isscalar()` is a function in the NumPy library of Python that is used to determine whether a given input is a scalar or not. A scalar is a single value, as opposed to an array or a more complex data structure. The function returns `True` if the input is a scalar and `False` otherwise. It is a useful tool for type checking in numerical and scientific computing applications where distinguishing between scalar and non-scalar values is often necessary.

Python numpy.isscalar() Function Example

Here are several commonly used examples of the `numpy.isscalar()` function, which we will elucidate for better understanding.

  • Scalar Check in Python
  • Check if a Variable is a Scalar
  • Validating Input Type in a Function
  • Using isscalar() in Conditional Logic

Scalar Check in Python

In this example Python program uses NumPy's `isscalar()` function to check if an input array `[1, 3, 5, 4]` is a scalar and prints the result. It further demonstrates the function's use by checking the scalar status of an integer (`7`) and a list (`[7]`), providing output indicating whether each input is a scalar.

Output :

Input array : [1, 3, 5, 4]
Is scalar : False
isscalar(7) : True
isscalar([7]) : False

Check if a Variable is a Scalar

In this example code uses NumPy's `isscalar()` to check if variables `x` (an integer) and `y` (a list) are scalars. It prints the results, indicating that `x` is a scalar (`True`) and `y` is not a scalar (`False`).

Output:

x is a scalar: True
y is a scalar: False

Validating Input Type in a Function

In this example code defines a function `calculate_square` that takes a parameter `value`, checks if it is a scalar using `numpy.isscalar()`, and returns the square if it is. If the input is not a scalar, it raises a `ValueError`. It then calculates and prints the square of the scalar value 5, demonstrating the function's usage.

Output:

Square of 5: 25

Using isscalar() in Conditional Logic

In this example code defines a function `process_data` that checks if the input is a scalar or a NumPy array, printing the corresponding message. It's demonstrated with an integer (prints the value), a NumPy array (prints "Processing NumPy array data"), and a string (prints "Unsupported data type").

Output:

Processing scalar data: 10
Processing NumPy array data
Processing scalar data: Invalid
Comment