Skip to content

Check null Python | Example code

  • by

There’s no null in Python; instead, None is used. The best way to check things for “Noneness” is to use the identity operator, is:

if foo is None:
    ...

Read: None vs Null

Example check null Python

Simple example code to test for None use the is the operator.

a = None

if a is None:
    print("a is", a)
else:
    print("Value of a", a)

Output:

Check null Python

Python if not null or empty

Even space or defined variable is counted as a not a null variable in python. You should define it as a None.

var = " "

if var is not None:
    print('Var is not null')

Output:

Var is not null

Do comment if you have any doubts or suggestions on this Python Basic topic

Note: IDE: PyCharm 2021.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.