A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/python/python_cmath_nan_constant.htm below:

Python cmath.nan Constant

Python cmath.nan Constant

The Python cmath.nan constant returns a floating point nan(Not a Number). This constant is represented as NaN. It is a predefined value used to denote undefined numerical values, practically these are raised by mathematical operations.

NaN is used to represent the result of operations that are mathematically undefined, such as zero by zero, square root of a negative number.

Syntax

Following is the basic syntax of the Python cmath.nan constant −

cmath.nan
Example 1

In the below example we are creating a NaN value. We will initialize a variable cmath.nan constant that indicates the numeric data.

import cmath
x = cmath.nan
print("The value of x is:", x)
Output

Following is the output of the above code −

The value of x is: nan
Example 2

Now, we are are creating a new list "valid_data" containing only the non-NaN values from the original list.

import cmath
data = [4.6, cmath.nan, 7.7, cmath.nan, 2.9]
valid_data = [x for x in data if not cmath.isnan(x)]
print("The valid data points are:", valid_data)
Output

The Result obtained is as follows −

The valid data points are: [4.6, 7.7, 2.9]
Example 3

Here, we are calculating sum of NaN numbers (Sum of any NaN numbers is always a NaN).

import cmath
x = 10
y = cmath.nan
z = 20
w = cmath.nan
result = x + y + z +w
print("The result of the calculation is:", result)
Output

The output is produced as follows −

The result of the calculation is: nan
Example 4

Now, we are creating list containing numeric values, including NaN values using cmath.nan. We will compare all the values from the list and filter the NaN values.

import cmath
values = [15, cmath.nan, 63, cmath.nan, 25]
filtered_values = [x for x in values if not cmath.isnan(x)]
print("The list after filtering out NaN values is:", filtered_values)
Output

We will get the output as follows −

The list after filtering out NaN values is: [15, 63, 25]

python_modules.htm


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4