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))
def cal(x): if x > 1: return x else: return None print(cal(1))
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))
Do comment if you have any doubts and suggestions on this Python basic tutorial.
Note: IDE: PyCharm 2021.1.3 (Community Edition)
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.