How To Return A Default Value If None In Python

Return a default value if None in Python

Follow this article to learn how to return a default value if None in Python with the explanation and examples below.

None In Python

None is the special object that is used to assign a null value, no value at all or null object. None is the datatype of the NoneType class. You can assign the value None to any variable. None is not 0, False or empty String. None is only None.

Look at the example below to learn more about None in Python.

x = None
print(x) # None
print(type(x)) # <class 'NoneType'>

if x :
   print('None is True')

elif x == False:
   print('None is False')

elif x == '':
   print('None is the empty String')

elif x ==0 :
   print('None is 0')

else:
   print('None is only None')

Output 

None
<class 'NoneType'>
None is only None

Return A Default Value If None In Python

There are some ways that can help you return a default value if None in Python.

With The Function

You can return a default value if None in Python with a function. Firstly, you have to assign the value of the parameters as None in the function. So when you call this function, if you assign a value to the function, this function will run correctly. Otherwise, the value is automatically assigned as None. So you can return a default value if you encounter the second case.

Look at the example below to learn more about this solution.

# The function that calculates the square of the number
def square (x = None):
    if x == None:
        return 0
    return x*x

print(square(10))
print(square())

Output 

100 
0

As you can see, if you don’t assign the value for the parameter. The function will automatically assign the value None for the parameter and return a default value.

In The Dictionary 

As you know, the dictionary in Python has two parameters: key and value. You can get the value of the key in the dictionary by the get() method built in Python. But if the key does not exist, can you get a value of the key? 

Look at the example below.

color = {'yellow':1,'red':2,'black':3,'blue':4,'pink':5,'white':6}

print(color.get('yellow'))
print(color.get('green'))

Output 

1
None

As you see, the function will return a None value if the key does not exist. But you want to return a value of 0.

You can use the get() method with two parameters to solve this problem.

Syntax:

dict.get(key, defaultValue)

Parameters:

  • dict: The current dict.
  • key: The key you want to get the value.
  • defaultValue: The value if the key does not exist.

Return value:

The value of the key or the default value.

Look at the example below to learn more about this solution.

color = {'yellow':1,'red':2,'black':3,'blue':4,'pink':5,'white':6}

print(color.get('yellow'))
print(color.get('green'))

Output 

1
None

Summary

These are some methods that can help you return a default value if None in Python. After learning how to return a default value in the function and the dictionary, we are sure that you can solve this problem in another case. We hope this tutorial is helpful to you. Thanks!

Maybe you are interested:

Leave a Reply

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