python math.isnan()方法,判断数值是否为nan

math.isnan()方法

python math模块的isnan()方法,可以判断一个python的对象是否为real number,比如int、float等类型的值,如果不是,返回True,如果是则返回False。


isnan()语法

isnan(x, /)

python源码中对isfinite()方法的介绍:

    Return True if x is a NaN (not a number), and False otherwise.

参数

参数描述
x必须参数,为real number类型。

返回值

Python bool类型True or False。


math.isnan()实例代码

>>> import math
>>> math.isnan(0)
False
>>> math.isnan(2)
False
>>> math.isnan(math.nan)
True
>>> math.isnan([])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: must be real number, not list


全栈后端 / Python库 :









Copyright © 2022-2024 笨鸟工具 x1y1z1.com All Rights Reserved.