The Python return
key phrase means that you can outline the return worth of a operate (e.g., return X
). In case you don’t explicitly set a return worth otherwise you omit the return
assertion, Python will implicitly return the next default worth: None
.
There are three circumstances of what a operate can return:
- Case 1: The operate has a
return
assertion that returns an express worth. - Case 2: The operate has a
return
assertion that returns explicitly nothing. It returnsNone
. - Case 3: The operate doesn’t have a
return
assertion. The operate in that case implicitly returnsNone
.
Let’s have an instance of all three circumstances subsequent!
Case 1: The operate has a return
assertion that returns an express worth.
def f1(): return 42 print(f1()) # 42
Case 2: The operate has a return
assertion that returns explicitly nothing. It returns None
.
def f2(): return print(f2()) # None
Case 3: The operate doesn’t have a return
assertion. The operate in that case implicitly returns None
.
def f3(): move print(f3()) # None
Be happy to take a look at our Python cheat sheets to find out about all of the Python fundamentals and key phrases and methods:
Whereas working as a researcher in distributed programs, Dr. Christian Mayer discovered his love for educating laptop science college students.
To assist college students attain increased ranges of Python success, he based the programming schooling web site Finxter.com. He’s creator of the favored programming ebook Python One-Liners (NoStarch 2020), coauthor of the Espresso Break Python collection of self-published books, laptop science fanatic, freelancer, and proprietor of one of many high 10 largest Python blogs worldwide.
His passions are writing, studying, and coding. However his biggest ardour is to serve aspiring coders by means of Finxter and assist them to spice up their expertise. You may be part of his free electronic mail academy right here.