Return nothing Python | Example code

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 add 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.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.