Search⌘ K
AI Features

Nan-reflexivity

Understand Python's handling of NaN and infinity by exploring their special behaviors and effects on reflexivity in comparisons. Learn how these values differ from typical data types and affect collection operations, improving your grasp of Python's unique quirks.

We'll cover the following...

Let’s see what Python thinks about NaN and infinity.

1.

What do you think about these assignments?

Python 3.5
a = float('inf')
b = float('nan')
c = float('-iNf') # These strings are case-insensitive
d = float('nan')

Let’s observe some behaviors in the terminal below:

Terminal 1
Terminal
Loading...

2.

Let’s explore further.

Python 3.5
x = float('nan')
y = x / x
print(y is y) # identity holds
print(y == y) # equality fails of y
print([y] == [y]) # but the equality succeeds for the list containing y

Explanation

  • 'inf'</ ...

...