How can I check for NaN in Python?

0 votes

float('nan') results in Nan (not a number). But how do I check for it? Should be very easy, but I cannot find it

Aug 16, 2018 in Python by bug_seeker
• 15,350 points
6,503 views

1 answer to this question.

0 votes

math.isnan()

Checks if the float x is a NaN (not a number). NaNs are part of the IEEE 754 standards. Operation like but not limited to inf * 0, inf / inf or any operation involving a NaN, e.g. nan * 1, return a NaN.

New in version 2.6.

>>> import math
>>> x=float(
'nan')
>>> math.isnan(x)

True
>>>

answered Aug 16, 2018 by Priyaj
• 56,520 points

Related Questions In Python

0 votes
1 answer

how do i check for exceptions in python?

use self.assertRaises method as a context manager. def ...READ MORE

answered Mar 12 in Python by Mohammad
• 3,060 points
45 views
0 votes
1 answer

How can I deal with python eggs for multiple platforms in one location?

Try virtualenv : http://pypi.python.org/pypi/virtualenv This helps you create isolated ...READ MORE

answered May 28 in Python by SDeb
• 13,160 points
24 views
0 votes
1 answer

How can I change the iteration variable inside a for loop in python?

Assume you have a list of lists: my_list ...READ MORE

answered Jun 10 in Python by Shubham Bansal
33 views
+3 votes
7 answers

How can I rename a file in Python?

yes, you can use "os.rename" for that. ...READ MORE

answered Mar 31, 2018 in Python by DareDev
• 6,810 points
128 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 216 views
+4 votes
6 answers
+2 votes
3 answers

How can I play an audio file in the background using Python?

down voteacceptedFor windows: you could use  winsound.SND_ASYNC to play them ...READ MORE

answered Apr 3, 2018 in Python by charlie_brown
• 7,720 points
1,678 views
0 votes
1 answer

How can I print variable and string on same line in Python?

Use , to separate strings and variables while printing: print ...READ MORE

answered Sep 17, 2018 in Python by Priyaj
• 56,520 points
62 views