return keyword allows you to define the return value of a function (e.g.,
return X). If you don’t explicitly set a return value or you omit the
return statement, Python will implicitly return the following default value:
There are three cases of what a function can return:
- Case 1: The function has a
returnstatement that returns an explicit value.
- Case 2: The function has a
returnstatement that returns explicitly nothing. It returns
- Case 3: The function does not have a
returnstatement. The function in that case implicitly returns
Let’s have an example of all three cases next!
Case 1: The function has a
return statement that returns an explicit value.
def f1(): return 42 print(f1()) # 42
Case 2: The function has a
return statement that returns explicitly nothing. It returns
def f2(): return print(f2()) # None
Case 3: The function does not have a
return statement. The function in that case implicitly returns
def f3(): pass print(f3()) # None
Feel free to check out our Python cheat sheets to learn about all the Python basics and keywords and tricks:
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.