# How to check if a number is NAN or INF in python ?

Examples of how to check if a number is NAN or INF in python### Vérifier si un nombre est égale à 'NAN'

### Check if a number is 'NAN'

To check if a number is 'NAN', a solution is to use the math module with the function isnan()

````import numpy as np`
`import math`

`x = 2.0`

`math.isnan(x)`
```

gives

````False`
```

while

````x = np.nan`

`math.isnan(x)`
```

returns

````True`
```

### Check if a number is 'INF'

To check if a number is 'INF', a solution is to use the math module with the function isinf()

````import numpy as np`
`import math`

`x = 2.0`

`math.isinf(x)`
```

gives

````False`
```

while

````x = np.inf`

`math.isinf(x)`
```

returns

````True`
```

### Check if a number is finite

Another solution, it is possible to check if a number is finite with the function isfinite()

````>>> import numpy as np`
`>>> x = np.nan`
`>>> y = np.inf`
`>>> x`
`nan`
`>>> y`
`inf`
`>>> np.isfinite(x)`
`False`
`>>> np.isfinite(y)`
`False`
```

### Check is a variable is a number with isinstance

To check if a variable is a number (int or float for example) a solution is to use isinstance:

````x = 1.2`

`isinstance(x, (int, float))`
```

gives here

````True`
```

while

````x = 'abcd'`

`isinstance(x, (int, float))`
```

returns

```` false`
```