[Solved] Why does my function print None in Python

avatar

Borislav Hadzhiev

Last updated: Jun 15, 2022

banner

Check out my new book

[Solved] Why does my function print None in Python #

Functions often print None when we pass the result of calling a function that doesn't return anything to the print() function. All functions that don't explicitly return a value, return None in Python.

main.py
def example(): print('hello world') print(example()) # 👇️ Output # hello world # None

Notice that we called the print() function twice.

When we call the example() function, the print() function gets called with hello world, and then we print the result of calling the example() function.

Because the example function doesn't return anything, it ends up implicitly returning None, so None gets printed.

If we were to return a value from the function, the value would get printed.

main.py
def example(): print('hello world') return 'bye' print(example()) # 👇️ Output # hello world # bye

We used the return statement to return a value from the function.

Now when the example() function is called, we print the strings hello world and bye, instead of None.

Note that all functions that don't explicitly return a value, end up implicitly returning None.

If you don't want to return anything from the function, remove the print() call when calling the example() function.

main.py
def example(): print('hello world') example() # 👇️ Output # hello world

There is no point in printing the result of calling a function that doesn't return anything because we're always going to print None.

The most common sources of None values are:

  1. Having a function that doesn't return anything (returns None implicitly).
  2. Explicitly setting a variable to None.
  3. Assigning a variable to the result of calling a built-in function that doesn't return anything.
  4. Having a function that only returns a value if certain condition is met.
Note that there are many built-in functions (e.g. sort()) that mutate the original object in place and return None.

Another common source of None values is having a function that returns a value only if a condition is met.

main.py
def get_list(a): if len(a) > 3: return a # 👇️ None my_list = get_list(['a', 'b']) print(my_list) # 👉️ None

The if statement in the get_list function is only ran if the passed in argument has a length greater than 3.

In all other cases, the function doesn't return anything and ends up implicitly returning None.

To get around this, we could return a default value if the condition is not met, e.g. an empty string, an empty list, 0, or any other value that suits your use case.

main.py
def get_list(a): if len(a) > 3: return a return [] # 👈️ return empty list if condition not met # 👇️ [] my_list = get_list(['a', 'b']) print(my_list) # 👉️ []

Now the function is guaranteed to return a value regardless if the condition is met.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.