Skip to content

Return nothing Python | Example code

  • by

There is no such term as “returning nothing” in Python. Every function returns some value. If no explicit return statement is used, Python treats it as returning None.

Return nothing python examples

Simple example code.

To literally return ‘nothing’ use the pass keyword, it returns the value None if added in a function(Functions must return a value, so why not ‘nothing’). You can do this explicitly and return None yourself though.

def cal(x):
    if x > 1:
        return x
    else:
        pass


print(cal(1))

OR

def cal(x):
    if x > 1:
        return x
    else:
        return None


print(cal(1))

Output:

Return nothing Python

Does ‘return None’ in python not recommended?

Answer: There is nothing wrong with returning None. In most cases, you don’t need to explicitly return None. Python will do it for you.

def foobar(check):
    if check:
        return "Hello"


print(foobar(False))

Output: None

Do comment if you have any doubts and suggestions on this Python basic tutorial.

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