![]() |
Python is a dynamically typed language with multiple concepts which might get confusing as we get to the computational parts of the Python language. Understanding basic concepts becomes a part of getting to know about the thought process of working with such concepts. One of such ambiguities arrives when comparing two same-sounding but different concepts in Python named NaN value and None value. In this article, we will be discussing the difference between the NaN value and the None value in Python. NaN in PythonNaN or Not a Number is considered to be a value that is either undefined or missing, for example, in any dataset any values that are missing could be defined as NaN value, the NaN value works as a placeholder here, which some other value could later replace. However, it still has a mathematical significance to it, it usually works as a placeholder when there is a computational problem and the value of every cell is important, therefore, considering the value to be NaN keeps the data consistent. The NaN value in Python could be represented as: float("nan") NaN values are commonly used for scientific computational purposes since a lot of real-world data might miss some of the data values. For example, if we want to find out the square root of a negative number let’s say -5 the output will be NaN value in NumPy. Python3
Output: The resulting value is 'nan' which has the datatype <class 'numpy.float64'>
Identifying if NaN Values are Present in the DatasetWe can identify if a NaN value is present in the dataset by importing it into the Pandas library, here is a code example to exactly do that: First we will be importing important libraries NumPy for mathematical computing and Pandas for data manipulation and then creating a Python dictionary and converting it into a Pandas DataFrame and then printing it. Python3
Output: DataFrame: None in PythonIn Python, the None value describes the empty value or when the value of something is not present, it is commonly used when value is absent in a variable or parameter. None value also appears for a function which does not return any particular value. For example, let’s create a function with no return value, we will use pass statement in the function: Python3
Output: The result is of the datatype: <class 'NoneType'>
Difference Between NAN and None in Python
Below are some of the examples indicating the difference between Nan and None in Python: Type CheckThrough this code example we will be differentiating between NaN and None value with the help of their datatype, showcasing the difference in their datatype helps us understand that both the values have different significance. Python3
Output: Value: None is of type: <class 'NoneType'> Equality CheckIn this code example, we will be differentiating between NaN and None with the help of their particular equality checking modes, the equality of None could be checked with the help of ‘==’ operator and the equality of NaN could be checked with the help of ‘math.isnan()‘ method. Python3
Output: Equality Check for None: True Arithematic OperationIn this code example we will be differentiating NaN and None values based on their behaviour while interacting with arithematic operators. The None variable interacting with integer will produce an error, meanwhile the NaN value does not gets affected with arithematic operation. Python3
Output: Result: Error: unsupported operand type(s) for +: 'NoneType' and 'int' |
Reffered: https://www.geeksforgeeks.org
Python |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |