Skip to content

Python None vs null | Basics

  • by

The first thing to know is there’s no null keyword in Python. Instead of null, there’s a None keyword used in Python. so there is no comparison between Python None vs null.

In other programming languages null is often defined to be 0, but in Python used None to define null objects and variables. But None is not defined to be 0 or any other value.

So, in Python, you should use None it to represent the absence of a value, not null. If you try to use null in Python code, you will get a NameError because null is not defined in the language.

Here’s an example of how to use None in Python:

def print_name(name):
    if name is None:
        print("Name is not provided")
    else:
        print("Name is:", name)

print_name("John")  
print_name(None)    

Output:

Python None vs null

As you can see, None is used to represent the absence of a value for the name parameter. If we call the function with None, it will print the appropriate message.

Note: None and null difference and there is no null value in Python, you can use None like null.

Do comment if you have any doubts or suggestions on this Python 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