Like we have NUL, NULL, or null in other programming languages, in Python we have None which is used to represent the absence of a value.
In order to check if a particular object is None or not, we can make use of the If condition as below,
-
Syntax 1:
if variable is not None:
Syntax 2:
def function(object):
if object is not None:
# Your code Logic
else:
# Your code Logic
It is always better to look at the PEP (Python Enhancement Proposals) Guides to know what they say.
As per PEP-8
# Correct:
if foo is not None:
# Wrong:
if not foo is None:
Read More: Programming Recommendations
Now let's take a look at a few examples.
def check_for_none(object):
if object is not None:
print(f"The object is: {object}")
else:
print("The object is None!")
check_for_none(42)
check_for_none("Hello")
check_for_none(None)
Output:
The object is: 42
The object is: Hello
The object is None!

Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!