In Post #91, we learned how to use the return
statement to send a single value back from a function. In Post #92, we saw that if we don’t return anything, the function implicitly returns None
. This covers returning one value or no value, but what if a function needs to send back multiple pieces of information?
In this post, we’ll revisit a powerful Python pattern we first saw in Post #61: how functions can appear to “return multiple values” by automatically packing them into a tuple.
The Trick: Returning One Collection
Let’s be precise: a function can only ever return one object. The “trick” to returning multiple values is to return a single collection object that contains all the values you need. In Python, the default and most convenient container for this is a tuple.
Automatic Tuple Packing
When you write a return
statement with multiple values separated by commas, Python performs a neat trick called automatic tuple packing. It gathers all the values you listed and creates a single tuple from them behind the scenes.
This means that writing:
return value1, value2
is just a cleaner, more convenient way of writing:
return (value1, value2)
A Practical Example: Parsing a Name
Let’s write a function that takes a full name string and returns the first and last names as separate values.
def parse_full_name(full_name):
"""Takes a full name string and returns the first and last names."""
# The .split() method is a handy string tool that breaks a
# string into a list of words, using spaces as the separator.
parts = full_name.split()
first_name = parts[0]
last_name = parts[-1] # Use negative indexing for the last name
# Python packs these two strings into a tuple and returns it
return first_name, last_name
When we call this function, we can use the sequence unpacking technique we learned in Post #60 to assign the returned values directly to separate variables.
user_full_name = "Grace Hopper"
# The function returns a tuple, which is immediately unpacked
# into the 'first' and 'last' variables.
first, last = parse_full_name(user_full_name)
print(f"First name: {first}")
print(f"Last name: {last}")
The output will be:
First name: Grace
Last name: Hopper
To prove that the function is indeed returning a tuple, we can assign its return value to a single variable and inspect it.
name_parts_tuple = parse_full_name("Alan Turing")
print(f"\nThe returned object is a: {type(name_parts_tuple)}")
print(f"The value is: {name_parts_tuple}")
This clearly shows the underlying mechanism:
The returned object is a: <class 'tuple'>
The value is: ('Alan', 'Turing')
What’s Next?
Returning multiple values by packing them into a tuple, and then immediately unpacking them into separate variables, is a common, powerful, and highly “Pythonic” pattern. It allows functions to provide rich, structured information back to the caller in a clean and readable way.
We’ve now seen how data can be passed into functions (as arguments) and passed out of functions (as return values). But where do the variables we create inside a function “live”? Can we access them from outside the function? This brings us to a crucial concept in programming. In Post #94, we will begin our introduction to variable scope.
Author

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com