Return. This is a Python keyword. We use return to signal the end of a method. We can place one or more return values in a method—they are executed when encountered.
In Python, we find different terms for things like "void" methods. And for "no value" we have the special None value. Tuples can return multiple things at once.
No return value. Methods in Python do not need to return a value. If we use the "return" statement alone, no value is returned. This is called a void method in other languages.
Note Clarity of code is important. Sometimes, having more symmetry in a method (where "return" is used for all paths) is a clearer design.
Return none. When a method has no return value specified, and it terminates, the None value is returned. We can test for None. Here, example() returns None unless "x" is greater than zero.
def example(x):
# Return a value only if argument is greater than zero.
if x > 0:
return x
print(example(0))
print(example(1))None
1
Return multiple values. A tuple is a small container for more than one value. We can return 2 or more values from a method at once with a tuple. These values can be unpacked.
def get_names_uppercase(a, b):
# This method returns 2 strings in a tuple.
return (a.upper(), b.upper());
# Get tuple from the method.
result = get_names_uppercase("vidiadhar", "naipaul")
print(result)
# Extract first and second return values.
first = result[0]
second = result[1]
print(first)
print(second)('VIDIADHAR', 'NAIPAUL')
VIDIADHAR
NAIPAUL
The return keyword is an essential part of Python programs. With it we direct the flow of control. We can return multiple values with a special type like a tuple.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Apr 15, 2025 (edit).