World's Best AI Learning Platform with profoundly Demanding Certification Programs
Designed by IITians, only for AI Learners.
Designed by IITians, only for AI Learners.
New to InsideAIML? Create an account
Employer? Create an account
What does the * symbol do in the return statement of a Python function?
In a Python function, the * symbol in the return statement is used to return multiple values as a tuple. This is known as tuple packing.
For example, consider the following function that takes two arguments and returns their sum and difference:
def sum_and_difference(a, b): return a+b, a-b
In this function, the return statement returns two values a+b and a-b separated by a comma. This means that the values will be returned as a tuple, and the tuple will be unpacked into two separate variables when the function is called:
result = sum_and_difference(5, 3) print(result) # Output: (8, 2) sum, diff = sum_and_difference(5, 3) print(sum) # Output: 8 print(diff) # Output: 2
In the second example, the returned tuple is automatically unpacked into the variables sum and diff.