Python does not have a "null" value. However, you can use the "None
" keyword instead to represent null values, which implies absence of value. None
can be used with variables, function/method arguments or return value, etc.
For example, assigning None
to a variable implies it has no value (or "null" value):
foo = None print(foo) # None
To check whether a variable is None
or not, you can use the is
(or is not
) operator.
Similarly, you could assign None
to function arguments (to serve as a default value when an argument is not passed to the function) or as a return value from a function:
def foo(param = None): if param is None: return 'empty' print(foo()) # 'empty'
def foo(): return None print(foo()) # None
This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.