# How to set variable to Nan in Python

In this post, we are going to learn How to set variables to Nan in Python. The Nan is not a number that is used to represent the missing or undefined values. It is particularly floating-point values. The Nan is also set to variables that do not have values that have yet not computed.

## How to set variable to Nan in Python

The python built-in float() function is used to assign Nan values to a variable. We have to just pass the string “Nan” as a parameter to the float function. The “Nan” is case insensitive so we can use the following format to assign to a variable. Let us understand with the below examples

```float("nan")
float("+nan")
float("-nan")
float("nAn")
float("nAN")
float("naN")
float("Nan")
float("NAn")
float("NAN")
float("NaN")

```
```negInf = float('nan')
print('The Value you have assigned is:',negInf)
```

Output

```The Value you have assigned is : nan
```

### 2. How to Set a variable Nan in Python using decimal

In this python program, We have used a decimal module to assign a variable to nan values in python. First, we have to import the decimal module and used Decimal(“Nan”) to assign variables to the nan value.

```from decimal import Decimal

nanval = Decimal("Nan")
print ('Value you have assigned is :',nanval )

```

Output

```Value you have assigned is : NaN
```

### 3.. How to Set a variable Nan Python using decimal

The python math module attribute is also a great choice to set a variable to nan. First, we’ll import the math module and assign the variable to nan using “math. nan”

```import math

nanval = math.nan

print ('Value you have assigned :',nanval )

```

Output

```Value you have assigned is : NaN
```

### 4. Set a variable Nan Python using numpy

The python numpy module can also be used to assign a variable to nan in Python. First, we have to install the numpy library if not already installed on our system and Import it to our program by using “import numpy as np” the numpy library attribute np.nan is used to assign a variable to nan.

```import numpy as np

nanval = np.nan
print ('Value is nan:',np.isnan(nanval) )
print ('Value you have assigned :',nanval )
```

Output

```Value is nan: True
Value you have assigned : nan
```

### Summary

In this post, we have learned how To Set Variable To Nan In Python by using the 4 different ways with the help of modules numpy, math, decimal, and python built-in float function.