Skip to content

What is null in Python?

[

Null in Python: Understanding Python’s NoneType Object

Table of Contents


If you have experience with other programming languages, like C or Java, then you’ve probably heard of the concept of null. Many languages use this to represent a pointer that doesn’t point to anything, to denote when a variable is empty, or to mark default parameters that you haven’t yet supplied. null is often defined to be 0 in those languages, but null in Python is different.

Python uses the keyword None to define null objects and variables. While None does serve some of the same purposes as null in other languages, it’s another beast entirely. As the null in Python, None is not defined to be 0 or any other value. In Python, None is an object and a first-class citizen!

In this tutorial, you’ll learn:

  • What None is and how to test for it
  • When and why to use None as a default parameter
  • What None and NoneType mean in your traceback
  • How to use None in type checking
  • How null in Python works under the hood

Understanding Null in Python

None is the value a function returns when there is no return statement in the function:

def has_no_return():
pass
has_no_return()
print(has_no_return())

When you call has_no_return(), there’s no output for you to see. When you print a call to it, however, you’ll see the hidden None it returns.

In fact, None so frequently appears as a return value that the Python REPL won’t print None unless you explicitly tell it to:

None
print(None)

None by itself has no output, but printing it displays None to the console.

Interestingly, print() itself has no return value. If you try to print a call to print(), then you’ll get None:

print(print("Hello, World!"))

It may look strange, but print(print("...")) shows you the None that the inner print() returns.

None is also often used as a signal for missing or default parameters. For instance, None appears twice in the docs for list.sort():

help(list.sort)

Here, None is the default value for the key parameter as well as the type hint for the return value.

Using Python’s Null Object None

Often, you’ll see the keyword None used to define a variable without assigning it a specific value. This is useful when you want to initialize a variable that may or may not need a value later on. Here’s an example:

result = None
if condition:
result = calculate_result()

In this case, result is initially set to None and later assigned a value if a certain condition is met. This allows you to check if result has been assigned a value or not by comparing it to None.

Declaring Null Variables in Python

To declare a null variable in Python, you simply assign it the value None. Here’s an example:

x = None

In this example, x is assigned the value None, indicating that it is empty or has no value.

Using None as a Default Parameter

You can use None as a default value for function parameters. This allows you to define a parameter without requiring the caller to provide a value. Here’s an example:

def greet(name=None):
if name is None:
name = "Stranger"
print(f"Hello, {name}!")
greet()
greet("Alice")

In this example, the greet() function has a parameter name with a default value of None. If no value is provided when calling the function, it will use the default value “Stranger”. Otherwise, it will use the provided value.

Using None as a Null Value in Python

You can use None to represent a null value in Python. This can be useful when working with data that may have missing values or when you need to indicate the absence of a value. Here’s an example:

data = [1, None, 3, 4, None, 6]

In this example, the list data contains None values representing missing or null values in the data. You can then handle these values accordingly in your code.

Deciphering None in Tracebacks

When an error occurs in your code and a traceback is printed, you may see None mentioned in the traceback. This usually indicates that a function call did not return a value. Here’s an example:

def divide(a, b):
return a / b
result = divide(5, 0)

In this example, the divide() function is called with arguments 5 and 0, which should result in a division by zero error. When the error occurs, the traceback will show a mention of None to indicate that the function call did not return a value.

Checking for Null in Python

To check if a variable is None, you can use the is keyword. Here’s an example:

x = None
if x is None:
print("x is null")
else:
print("x is not null")

In this example, the is keyword is used to compare x to None. If x is None, it will print “x is null”. Otherwise, it will print “x is not null”.

Taking a Look Under the Hood

Internally, None is a singleton object in Python. This means that there is only one instance of None in memory, regardless of how many times it is used in your code. This can be useful for memory optimization and comparison operations. Here’s an example:

x = None
y = None
print(x is y) # True

In this example, x and y are both assigned the value None, but they refer to the same instance of the None object. Therefore, the comparison x is y returns True.

Conclusion

In Python, None is used to represent null objects and variables. It is an object and a first-class citizen in the language. None can be used to indicate missing or default parameters, as well as to represent the absence of a value in data. When working with None, it’s important to check for None using the is keyword and handle it accordingly in your code.

To deepen your understanding of None in Python, consider watching the related video course: Python’s None: Null in Python. This course provides additional examples and explanations to help you master the concept of None in Python.