How To Return Null in Python Discussed

Do you want to return NULL or None object in Python? This article will show you how you can return NULL in Python.

You can return the null value from a function in three ways that I will be discussing below. Based on different scenarios you can choose from which return value suits your code and you can use that in your Code.

Using return None statement

In Python when you will use statement return None then it is interpreted as you want to return a NULL object which is basically nothing or empty.

This statement means that there is a definite return from the function wherever it is used but it can be None or NULL. Let us see this in below example code and how you can use it.

#Below FunctioN Returns None when 
#Input to the Function is 0 else it
#Return the Input Number
def getInputNumber(number):
      if number == 0:
            return None
      else:
            return number

#Testing the above Function
print("The output for input {} is: {}".format(1, getInputNumber(1)))

print("The output for input {} is: {}".format(0, getInputNumber(0)))

Output:

The output for input 1 is: 1
The output for input 0 is: None

As you can see in the above code when 0 was entered it returned an empty object that is marked as None in Python. Note that in Python None works as Null.

How To Return Null in Python

Only Using Return Statement

In Python even if you use only the Return statement at end of your function it means that this function will return None Object and there is nothing to return. Now suppose you are using any variable to save the return of the function that is using only return statement then that object will have None.

Let us see in the below example code when to write only return statements.

#Below FunctioN Returns None when 
#Input to the Function is 0 else it
#Return the Input Number
def getInputNumber(number):
      if number == 0:
            return
      else:
            return number

#Testing the above Function
print("The output for input {} is: {}".format(2, getInputNumber(2)))

print("The output for input {} is: {}".format(0, getInputNumber(0)))

Output:

The output for input 2 is: 2
The output for input 0 is: None

As you can see in the above code we just returned the function without adding None to it and Python itself understood that you want to return a None object from the function at the given function.

Wrap Up

I hope you got the answer related to how you can return null in Python. To make more clear None is not completely the same as NULL as it is represented in other low-level languages but it is much closer to NULL.

None basically acts as an empty object and it will not cause any NULL pointer exceptions as we face such issues in low-level programming languages.

Let me know if you have a better method than the one shown in the above examples in the comments section. I will be happy to add it here and provide you with the credit for it.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. How to Split a String into a List of Words or Letters in Python
  2. Reading A Binary File In Python With Examples
  3. How To Check If The Variable is An Integer in Python
  4. Python – Check If String is an Integer or Float
  5. Python – Convert Bytes To String Quickly
  6. Python Print 2 Decimal Place Quickly

Leave a Reply

Your email address will not be published. Required fields are marked *