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)
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:
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.
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.
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.
How to return multiple values from a function in Python:
def get_values(): return 1, 'hello', 3.14 a, b, c = get_values() print(a, b, c) # Output: 1 hello 3.14
Tuple unpacking in Python function return:
def get_coordinates(): return 4, 7 x, y = get_coordinates() print(x, y) # Output: 4 7
Using namedtuples for multiple return values in Python:
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
Destructuring assignment with function return values in Python:
def get_info(): return ('John', 'Doe', 25) first_name, last_name, age = get_info() print(first_name, last_name, age) # Output: John Doe 25
Handling different data types in multiple return values:
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]
Type hints for functions with multiple return values in Python:
from typing import Tuple def get_values() -> Tuple[int, str, float]: return 1, 'hello', 3.14
Returning lists, dictionaries, and tuples from functions in Python:
def get_data(): return [1, 2, 3], {'name': 'Alice'}, (4, 5, 6)
Returning custom objects with multiple attributes in Python:
class Person: def __init__(self, name, age): self.name = name self.age = age def get_person(): return Person(name='Bob', age=28)
Multiple return values and variable unpacking in Python:
def get_values(): return 10, 20, 30 result = get_values() print(result) # Output: (10, 20, 30)
Returning functions with multiple values in Python:
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
Function chaining with multiple return values in Python:
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