In the previous article, we have discussed Python Program for isinf() Function
isnan() Function in Python:
The math.isnan() method determines whether a value is NaN (Not a Number).
If the specified value is a NaN, this method returns true; otherwise, it returns False.
Syntax:
math.isnan(x)
parameters:
x: This is required. It is a value to be checked.
Return Value: It returns a boolean value that is True if the value is NaN and False otherwise.
Examples:
Example1:
Input:
Given Value = -200.02 Given Value = 500 Given Value = math.nan Given Value = math.inf Given Value = NaN
Output:
False False True False True
Example2:
Input:
Given Value = -850.07 Given Value = nan
Output:
False True
Program for isnan() Function in Python
Method #1: Using Built-in Functions (Static Input)
Approach:
- Import math module using the import keyword.
- Give the number as static input and store it in a variable.
- Apply math.isnan() function to the given number to check if the given number is NaN(Not a Number) or not.
- Store it in another variable.
- Print the above result.
- Similarly, check for the other values and print the result.
- The Exit of Program.
Below is the implementation:
# Import math module using the import keyword. import math # Give the number as static input and store it in a variable. gvn_numb = -200.02 # Apply math.isnan() function to the given number to check if the given number # is NaN(Not a Number) or not. # Store it in another variable. rslt = math.isnan(gvn_numb) # print the above result. print("Checking if the above given number", gvn_numb, "is NaN or not :", rslt) # similarly check for the other values and print the result. print(math.isnan(500)) print(math.isnan(math.nan)) print(math.isnan(math.inf)) print(math.isnan(float('NaN')))
Output:
Checking if the above given number -200.02 is NaN or not : False False True False True
Method #2: Using Built-in Functions (User Input)
Approach:
- Import math module using the import keyword.
- Give the number as user input using the float(input()) function and store it in a variable.
- Apply math.isnan() function to the given number to check if the given number is NaN(Not a Number) or not.
- Store it in another variable.
- Print the above result.
- Similarly, check for the other values and print the result.
- The Exit of Program.
Below is the implementation:
# Import math module using the import keyword. import math # Give the number as user input using the float(input()) function and store it in a variable. gvn_numb = float(input('Enter some random number = ')) # Apply math.isnan() function to the given number to check if the given number # is NaN(Not a Number) or not. # Store it in another variable. rslt = math.isnan(gvn_numb) # print the above result. print("Checking if the above given number", gvn_numb, "is NaN or not :", rslt) # similarly check for the other values b = input('Enter some random value = ') print(math.isnan(float(b)))
Output:
Enter some random number = 380.0006 Checking if the above given number 380.0006 is NaN or not : False Enter some random value = nan True
Read all the mathematical functions available in Python and understand how to implement them in your program by using the tutorial of Python Mathematical Methods Examples.