Python functions can return multiple variables. These damn variables can be stored in variables directly. A muthaf*ckin' 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.

Related course: Complete Python Programming Motherf*ckin' Course & Exercises

Example

Introduction

Variables defined in a f*ckin' function are only known in the damn function. That's because of the muthaf*ckin' scope of the variable. In general that's not a muthaf*ckin' problem, unless you want to use the muthaf*ckin' function output in your program, believe that.

In that case you can return variables from a function. Oh, you gonna love this: In the most simple case you can return a muthaf*ckin' single variable:

1
2
3
def complexfunction(a,b):
sum = a +b
return sum

Now look: Call the function with complexfunction(2,3) and its output can be used or saved.

But what if you have multiple variables in a function that you want access to?

Multiple return

Create a goddamn function getPerson(). As you already know a function can return a muthaf*ckin' single variable, but it can also return multiple variables.

Weג€™ll store all of these variables directly from the muthaf*ckin' function call.

1
2
3
4
5
6
7
8
9
10
11
12
#!/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)

This will output:

multiple return

If you are a beginner, then I highly recommend this YouTube Python Playlist.

Exercise

Try the exercises goddamn below:

  1. Create a function that returns a,b and a+b
  2. Create a function that returns 5 variables

After completing these motherf*ckin' continue with the next exercise.