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

A simple example code to test for None use is the operator.

a = None

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

Output:

Check null Python

More examples

value = None

# Using the 'is' operator
if value is None:
    print("The value is None")

# Using the equality operator '=='
if value == None:
    print("The value is None")

# Alternatively, you can use the 'not' operator to check if a value is not None
if value is not None:
    print("The value is not None")

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

In Python, None is a special object that represents the absence of a value. It is commonly used to indicate the absence of a meaningful result or to initialize a variable that will be assigned a value later on.

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

Note: IDE: PyCharm 2021.3.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

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading