Friday, December 27, 2024
Google search engine
HomeLanguagesPython None Keyword

Python None Keyword

None is used to define a null value or Null object in Python. It is not the same as an empty string, a False, or a zero. It is a data type of the class NoneType object. 

None in Python

Python None is the function returns when there are no return statements.

Python3




def check_return():
    pass
print(check_return())


Output:

None

Null Vs None in Python

None – None is an instance of the NoneType object type. And it is a particular variable that has no objective value. While new NoneType objects cannot be generated, None can be assigned to any variable.

Null – There is no null in Python, we can use None instead of using null values.

Note: Python null is refer to None which is instance of the NoneType object type

Example:

Here we will both Null and None and we get the related output for these two statements. Where one of the outputs is Null is not defined.

Python3




print(type(None))
print(type(Null))


Output:

<class 'NoneType'>
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Input In [9], in <cell line: 2>()
     1 print(type(None))
----> 2 print(type(Null))
NameError: name 'Null' is not defined

Referring to the null object in Python

Assigning a value of None to a variable is one way to reset it to its original, empty state.

Example 1:

We will check the type of None

Python3




print(type(None))


Output:

<class 'NoneType'>

Example 2:

Declaring a variable as None.

Python3




var = None
 
# checking it's value
if var is None:
    print("var has a value of None")
else:
    print("var has a value")


Output:

var has a value of None

Note: If a function does not return anything, it returns None in Python.

RELATED ARTICLES

Most Popular

Recent Comments