Skip to content

Multiple return

Related course:

Multiple return

Python functions can return multiple variables. These variables can be stored in variables directly. A function is not required to return a variable, it can return zero, one, two or more variables.

This is a unique property of Python, other programming languages such as C++ or Java do not support this by default.

Example:

#!/usr/bin/env python3

def getPerson():
    name = "Leona"
    age = 35
    country = "UK"
    return name,age,country


name,age,country = getPerson()
print(name)
print(age)
print(country)

What you see if successful

You should see that the variables are directly saved from the function.

Exercises

Try the exercises below:

1. Create a function that returns a,b and a+b

After completing these continue with the next exercise.

Answers

Slices
Scope
Published inUncategorized

Be First to Comment

    Leave a Reply