Concepts / Working with Multiple Data Types

Working with Multiple Data Types

A function can return multiple values by packaging them into a single tuple object using the comma syntax in the return statement.

  • Programming

One Result with Several Parts

Many functions need to communicate more than one piece of information to the code that called them. A validation function might need to provide both a success status and an error message. A data-processing function might need to provide both a result and information about the operation. Python handles this by packaging the values into one tuple object.

A function that appears to return several values is returning one tuple containing those values. The tuple gives the caller one returned object whose elements can be accessed together or distributed into separate variables.

returnscontainscontainscheck_input()functionsuccesstuple elementerror messagetuple elementreturned tupleone object
What does a function return contain when values are separated by commas in the return statement?

The Return Tuple

The syntax for returning multiple values uses a return statement followed by the values separated by commas. The values are packaged into a tuple. Parentheses may be used around the values, but the comma is the important part that creates the tuple. This means the returned result is one tuple object, not several independent return channels.

python
Output
The function returns one tuple containing two elements: the status value 404 and the message "Page not found".
indexunpackunpackreturned tuple404, Page not foundresult[0]404errnum404errstrPage not found
What is the difference between keeping the returned tuple together and assigning its elements to separate variables?

Unpacking in Order

Tuple unpacking distributes the elements of one returned tuple into multiple variables in a single assignment statement. Python first executes the function and receives its tuple. It then compares the number of variables on the left side with the number of tuple elements. Finally, it assigns each element to the corresponding variable in order.

def get_error_details(): return 404, "Page not found" errnum, errstr = get_error_details()

position 0position 1404tuple position 0errnumreceives 404Page not foundtuple position 1errstrreceives Page not found
How does each value in a returned tuple move into its corresponding variable during unpacking?

Reading a Validation Result

A validation function returns a success status and an error message. How can the caller work with both results separately?

Package: The function returns the status and message as elements of one tuple.

Match: The caller writes two variables because the returned tuple contains two elements.

Assign: The first returned element goes to success, and the second goes to message.

The caller can use success and message as separate variables after one tuple-unpacking assignment.

Indexing or Unpacking

ApproachWhat it doesUseful when
IndexingKeeps the returned tuple as one object and accesses elements with bracket notationYou want the values together or need only some values
UnpackingDistributes tuple elements into separate variables in one assignmentYou know how many values are returned and want to use them individually
python

The first part of this example keeps the tuple in details and uses indexing to retrieve its elements. The second assignment immediately unpacks the returned tuple. Unpacking is generally preferred when the number of returned values is known and the values should be used as individual variables. Indexing remains useful when the tuple should stay together or only selected elements are needed.

Mistakes with Tuple Returns

  • Treating a tuple return as several independent return channels

    The function packages the values into one tuple object before the caller receives them.

    Fix: Receive the tuple as one object, or unpack its elements into variables.

  • Using the wrong number of variables during unpacking

    Tuple unpacking requires the variables on the left side to match the number of returned tuple elements.

    Fix: Use a matching number of variables, or keep the tuple together and access the needed elements by index.

  • Confusing indexing with unpacking

    Indexing accesses elements through positions, while unpacking assigns elements to variables in order.

    Fix: Use unpacking when the known returned values should be handled as separate variables.

  • Forgetting that order controls assignment

    Python assigns each element to the variable in the same position.

    Fix: Place the variables in the same order as the values returned by the function.

cannot matchunpacks in ordertwo tuple values404, Page not foundthree variablesmismatched counttwo tuple values404, Page not foundtwo variablesmatching count
What happens when the number of variables used for unpacking does not match the number of values in the returned tuple?

Practice the Choice

EASY

A function returns a tuple containing a processing result and metadata about how long the operation took. Describe which approach you would choose in each situation: keep both pieces together for later use, access only the first piece, or work with both pieces through descriptive variable names.

Hints
  • Keeping the returned values together suggests treating the tuple as one object.
  • Accessing only one element can be done with bracket notation.
  • Using both values as individual variables is the main use case for unpacking.

What do you think happens?

A function returns two tuple elements. If the caller writes two variables on the left side of the assignment, how are the elements assigned?

  • Both elements are assigned to the first variable
  • The first element goes to the first variable and the second goes to the second variable
  • The tuple remains inaccessible
  • The second element is discarded
Reveal answer

Answer: The first element goes to the first variable and the second goes to the second variable.

Tuple unpacking matches the number of variables with the number of tuple elements and assigns corresponding elements in order.

The Complete Pattern

  1. A function can communicate multiple values by packaging them into one tuple.
  2. The comma in the return statement is what matters for creating the tuple; parentheses are optional.
  3. Tuple unpacking distributes returned elements into separate variables in order.
  4. Use unpacking when the number of returned values is known and the values should be handled individually.
  5. Use indexing when you want to keep the tuple together or need only some of its elements.

Key Takeaways

  • Functions return one tuple object when multiple values are separated by commas in a return statement.
  • Tuple unpacking assigns tuple elements to separate variables in corresponding order.
  • The number of variables used for unpacking must match the number of returned tuple elements.
  • Indexing and unpacking are both valid, but unpacking is preferred when known values should be used individually.
  • Indexing is useful when values should remain together or only selected tuple elements are needed.