The Python 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: None
.
There are three cases of what a function can return:
- Case 1: The function has a
return
statement that returns an explicit value. - Case 2: The function has a
return
statement that returns explicitly nothing. It returnsNone
. - Case 3: The function does not have a
return
statement. The function in that case implicitly returnsNone
.
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 None
.
def f2(): return print(f2()) # None
Case 3: The function does not have a return
statement. The function in that case implicitly returns None
.
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: