python / beginner
Snippet
Multiple Return Values
A Python function can return multiple values separated by commas. These are returned as a tuple and can be unpacked into separate variables.
snippet.py
1
2
3
4
5
6
def get_coordinates():x = 10y = 20return x, ypos_x, pos_y = get_coordinates()
Breakdown
1
def get_coordinates():
Starts the definition of a function.
2
return x, y
Returns two values at once as a tuple.
3
pos_x, pos_y = get_coordinates()
Calls the function and assigns the two returned values to two variables.