{"id":24194,"date":"2021-10-14T09:17:50","date_gmt":"2021-10-14T03:47:50","guid":{"rendered":"https:\/\/python-programs.com\/?p=24194"},"modified":"2021-11-05T20:55:23","modified_gmt":"2021-11-05T15:25:23","slug":"python-program-for-isnan-function","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-isnan-function\/","title":{"rendered":"Python Program for isnan() Function"},"content":{"rendered":"
In the previous article, we have discussed Python Program for isinf() Function<\/a> The math.isnan() method determines whether a value is NaN (Not a Number).<\/p>\n If the specified value is a NaN, this method returns true; otherwise, it returns False.<\/p>\n Syntax:<\/strong><\/p>\n parameters:<\/strong><\/p>\n x:<\/strong> This is required. It is a value to be checked.<\/p>\n Return Value: <\/strong>It returns a boolean value that is True if the value is NaN and False otherwise.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 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<\/a>.<\/p>\n 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 …<\/p>\n
\nisnan() Function in Python:<\/strong><\/p>\nmath.isnan(x)<\/pre>\n
Given Value = -200.02\r\nGiven Value = 500\r\nGiven Value = math.nan\r\nGiven Value = math.inf\r\nGiven Value = NaN<\/pre>\n
False\r\nFalse\r\nTrue\r\nFalse\r\nTrue<\/pre>\n
Given Value = -850.07\r\nGiven Value = nan<\/pre>\n
False\r\nTrue<\/pre>\n
Program for isnan() Function in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Import math module using the import keyword.\r\nimport math\r\n# Give the number as static input and store it in a variable.\r\ngvn_numb = -200.02\r\n# Apply math.isnan() function to the given number to check if the given number\r\n# is NaN(Not a Number) or not.\r\n# Store it in another variable.\r\nrslt = math.isnan(gvn_numb)\r\n# print the above result.\r\nprint(\"Checking if the above given number\",\r\n gvn_numb, \"is NaN or not :\", rslt)\r\n# similarly check for the other values and print the result.\r\nprint(math.isnan(500))\r\nprint(math.isnan(math.nan))\r\nprint(math.isnan(math.inf))\r\nprint(math.isnan(float('NaN')))\r\n<\/pre>\n
Checking if the above given number -200.02 is NaN or not : False\r\nFalse\r\nTrue\r\nFalse\r\nTrue<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Import math module using the import keyword.\r\nimport math\r\n# Give the number as user input using the float(input()) function and store it in a variable.\r\ngvn_numb = float(input('Enter some random number = '))\r\n# Apply math.isnan() function to the given number to check if the given number\r\n# is NaN(Not a Number) or not.\r\n# Store it in another variable.\r\nrslt = math.isnan(gvn_numb)\r\n# print the above result.\r\nprint(\"Checking if the above given number\",\r\n gvn_numb, \"is NaN or not :\", rslt)\r\n# similarly check for the other values\r\nb = input('Enter some random value = ')\r\nprint(math.isnan(float(b)))<\/pre>\n
Enter some random number = 380.0006\r\nChecking if the above given number 380.0006 is NaN or not : False\r\nEnter some random value = nan\r\nTrue<\/pre>\n
\n