Multiple Return in Python

Level 3112 keystrokes

This function returns multiple values as a tuple. Python allows unpacking multiple return values into separate variables.

Python code

# Define a function that returns two values
1def min_max(nums):
# Return min and max as a tuple (comma creates tuple)
2 return min(nums), max(nums)
3
# Unpack the returned tuple into two variables
4lo, hi = min_max([3, 1, 4, 1, 5])
# Print both values using f-string
5print(f"Min: {lo}, Max: {hi}")

Output

Min: 1, Max: 5
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this Python code and how often they appear.

  • ,× 7
  • (× 5
  • )× 5
  • :× 3
  • _× 2
  • "× 2
  • {× 2
  • }× 2
  • [× 1
  • ]× 1

More Python examples at level 3

← Python typing practice