Python Tutorial

Python Variable

Python Operators

Python Sequence

Python String

Python Flow Control

Python Functions

Python Class and Object

Python Class Members (properties and methods)

Python Exception Handling

Python Modules

Python File Operations (I/O)

Python Function Returns Multiple Values

In Python, a function can return multiple values by separating them with commas. The multiple values will be packed into a tuple, which can then be unpacked by the caller. Here's an example of how to define, call, and use a function that returns multiple values:

  • Define a function that returns multiple values:
def min_max(numbers):
    min_value = min(numbers)
    max_value = max(numbers)
    return min_value, max_value

In this example, the min_max function takes a list of numbers as an argument and returns the minimum and maximum values in the list.

  • Call a function that returns multiple values:

When you call a function that returns multiple values, the returned values will be packed into a tuple.

numbers = [3, 5, 2, 8, 1, 7]
result = min_max(numbers)
print(result)  # Output: (1, 8)

In this example, we call the min_max function with a list of numbers, and the returned values (minimum and maximum) are packed into the result tuple.

  • Unpack multiple returned values:

You can use tuple unpacking to directly assign the returned values to separate variables.

min_value, max_value = min_max(numbers)
print(min_value)  # Output: 1
print(max_value)  # Output: 8

In this example, we unpack the returned values from the min_max function and assign them to the min_value and max_value variables.

In summary, to return multiple values from a function in Python, separate the values with commas in the return statement. The returned values will be packed into a tuple, which can then be unpacked and used by the caller as needed.

  1. How to return multiple values from a function in Python:

    • Description: Python functions can return multiple values using tuples, lists, or other data structures.
    • Code:
      def get_values():
          return 1, 'hello', 3.14
      
      a, b, c = get_values()
      print(a, b, c)  # Output: 1 hello 3.14
      
  2. Tuple unpacking in Python function return:

    • Description: Tuple unpacking allows you to assign multiple variables at once when calling a function that returns a tuple.
    • Code:
      def get_coordinates():
          return 4, 7
      
      x, y = get_coordinates()
      print(x, y)  # Output: 4 7
      
  3. Using namedtuples for multiple return values in Python:

    • Description: Namedtuples provide a way to define simple classes with named fields, making the code more readable.
    • Code:
      from collections import namedtuple
      
      def get_person():
          Person = namedtuple('Person', ['name', 'age'])
          return Person(name='Alice', age=30)
      
      person = get_person()
      print(person.name, person.age)  # Output: Alice 30
      
  4. Destructuring assignment with function return values in Python:

    • Description: Destructuring assignment allows you to assign elements of data structures to variables in a concise way.
    • Code:
      def get_info():
          return ('John', 'Doe', 25)
      
      first_name, last_name, age = get_info()
      print(first_name, last_name, age)  # Output: John Doe 25
      
  5. Handling different data types in multiple return values:

    • Description: Functions can return multiple values of different data types based on the logic within the function.
    • Code:
      def get_mixed_values():
          return 42, 'answer', [1, 2, 3]
      
      num, text, my_list = get_mixed_values()
      print(num, text, my_list)  # Output: 42 answer [1, 2, 3]
      
  6. Type hints for functions with multiple return values in Python:

    • Description: Type hints can be used to specify the expected data types of each value in the return statement.
    • Code:
      from typing import Tuple
      
      def get_values() -> Tuple[int, str, float]:
          return 1, 'hello', 3.14
      
  7. Returning lists, dictionaries, and tuples from functions in Python:

    • Description: Functions can return various data structures, including lists, dictionaries, and tuples.
    • Code:
      def get_data():
          return [1, 2, 3], {'name': 'Alice'}, (4, 5, 6)
      
  8. Returning custom objects with multiple attributes in Python:

    • Description: Custom objects with multiple attributes can be returned from functions for better organization of data.
    • Code:
      class Person:
          def __init__(self, name, age):
              self.name = name
              self.age = age
      
      def get_person():
          return Person(name='Bob', age=28)
      
  9. Multiple return values and variable unpacking in Python:

    • Description: Variable unpacking allows you to capture multiple return values in a single variable.
    • Code:
      def get_values():
          return 10, 20, 30
      
      result = get_values()
      print(result)  # Output: (10, 20, 30)
      
  10. Returning functions with multiple values in Python:

    • Description: Functions can return other functions with multiple values.
    • Code:
      def get_calculator():
          def add(x, y):
              return x + y
      
          def multiply(x, y):
              return x * y
      
          return add, multiply
      
      add_func, multiply_func = get_calculator()
      result_add = add_func(3, 5)
      result_multiply = multiply_func(3, 5)
      print(result_add, result_multiply)  # Output: 8 15
      
  11. Function chaining with multiple return values in Python:

    • Description: Functions with multiple return values can be chained together.
    • Code:
      def add_and_square(x, y):
          return x + y, (x + y) ** 2
      
      def double(value):
          return value * 2
      
      result_add, result_square = add_and_square(2, 3)
      final_result = double(result_square)
      print(final_result)  # Output: 50