← Back to Blog

Return two things at once

The problem...

Your function calculates two things. But return sends back one value and stops.

You'd need two functions. Or you'd print one and return the other. Neither feels clean.

The idea!

Python lets you return multiple values from a single function. Separate them with a comma. Unpack them on the other side.

The syntax

def function_name():
    return value_1, value_2
a, b = function_name()

Your first multiple return

def first_and_last(word):
    return word[0], word[-1]

first, last = first_and_last("RedHorn")
print(first)    # R
print(last)     # n

Two values returned. Two variables to receive them. First and last character — both at once.

BMI with category

def calculate_bmi(weight, height):
    bmi = weight / height ** 2
    if bmi < 18.5:
        category = "Underweight"
    elif bmi < 25:
        category = "Normal weight"
    elif bmi < 30:
        category = "Overweight"
    else:
        category = "Obese"
    return bmi, category

bmi, category = calculate_bmi(70, 1.75)
print(f"BMI: {bmi:.1f} — {category}")
# BMI: 22.9 — Normal weight

One function. Two pieces of information. One call.

Ignoring one of the values

bmi, category = calculate_bmi(70, 1.75)

# use only what you need
print(category)    # Normal weight

You unpack both — use only what you need. The rest is still there if you want it.

Heads up!

  • Separate return values with a comma — return a, b
  • Unpack in the same order — a, b = function()
  • Number of variables must match number of return values
  • You can ignore values you don't need — just unpack and leave them

The mindset shift

Stop thinking: "A function returns one thing."

Start thinking: "A function returns whatever it needs to. Pack it. Unpack it."

What you should understand now

  • return a, b — returns two values at once
  • x, y = function() — unpacks them into two variables
  • Order matters — first return value goes to first variable
  • Use only what you need — all values are available after unpacking
[ login to bookmark ] // copied! 20 views · 1 min
// resources
Code Example multiple_return_values.py
← prev Local vs global — where variables live next → Don't let errors crash your program
// 0 comments
// No comments yet. Be the first.
// leave a comment

// Your comment will appear after approval.