FAQ: Introduction to Functions - Multiple Returns

Without a receiver at the caller, it is not possible to access more than one return value in a print statement. Returns are not suspended in memory past their first access. They are consumed.

If there is a yes/no question to this, then, no.

1 Like

Thank you for the answer! :blush:

1 Like


Consider carefully what you want each function to do. Arbitrarily inserting return values is counterproductive. If a function is meant to return the result of a computation, then let its return be just that, and nothing else.

Under that premise this question would never have come up, although it is good that it did, and now we know.

Treat functions like mathematical equations.

f(x) = some function of x

y = f(some x)

Make them simple and always have a return so the expression they are used in makes sense and evaluates correctly. Don’t write programs inside a function. Write pure, concise algorithms that always have predictable results that are consistent across all inputs.

def mul(a, b):
    return a * b

y = mul(m, n) + o

Done and dusted.

The place to write a program is inside an object, namely a class. It may end up involving several class defined objects, but it is still a class. This form of composing code refines the method library so that they are all associated to a particular (one or other) class, not just functions out in the open namespace. Methods are expected to recognize their respective objects and know how to access their attributes. Functions aren’t that intelligent. They don’t know what we give them unless we tell them.

For example, let’s take multiplication, to continue from above. Python int instances have a __mul__ attribute. When the program sees us multiply two integers, it treats the first as self, and the second as other.

return self.value * other.value

It was the __mul__() method that performed that operation.

>>> a = 6
>>> b = 7
>>> a.__mul__(b)

That’s the progression when Python sees this…

a * b

Remember, built-ins act upon any object; methods act only upon instance objects.

Multiplication is a method, not a function.

The repeat operator

Python has extended the meaning of * in that if the types don’t match, and the opposing type is a character it will repeat their value N times (N being the number in the expression).

>>> 'O' * 8
>>> ['O'] * 8
['O', 'O', 'O', 'O', 'O', 'O', 'O', 'O']
>>> [['O' * 8] * 8][0]

Note that in the second example we introduced a list as the containing data structure.

Definitely play with this operator in the sandbox. Learn its behavior. Time well spent, imho.

I got the code correct. However, the console still kept saying that most_popular1 is not defined. It didn’t say anything about the other two variables.

This actually helped me to understand what was being asked of this and why we were doing it so thank you. I was getting myself really confused.

In the Multiple Return Exercises, we used the return feature to save multiple values from the function:
What is the data type of the returned values? Are the values stored separately or together as a tuple?

1 Like

It depends on the syntax used but functions in Python can arguably only return one object. If you’re using commas without any other literal syntax (like brackets or braces) and you store the return in a single object then you’re storing a tuple.

This is consistent with the way Python treats commas normally where a tuple is created if there’s no ambiguity-

x = 1, 2, 3,
print(type(x))  # Out: <class 'tuple'>

def func():
    return 1, 2, 3,

y = func()
print(type(y))  # Out: <class 'tuple'>

If you were to unpack the return into multiple arguments then they’d simply be references to whatever object they are-

a, b, c = func()
print(type(a))  # Out: <class 'int'>
1 Like

Hello tgrtim
That makes a lot of sense!
Thank you for promptly answering my question.

1 Like

Hello, dear community!
First of all, my apologies if this has been answered before. I have looked for a question with the exact same code and I couldn’t find it.
I wrote this:
def top_tourist_locations_italy():
first = “Rome”
second = “Venice”
third = “Florence”
return first, second, third
return top_tourist_locations_italy(most_popular1, most_popular2, most_popular3)

Apparently, it is correct, BUT my output should be:

I have no syntax errors or anything of the sort. Could someone please tell me what I missed here so I can have the desired output? Thank you very much in advance.

That line doesn’t look right.

1 Like

As I am working through the Introduction to Functions section on Returns and Multiple Returns, I feel like I am made to do extra work. Someone please explain why the extra steps are necessary?

EXAMPLE BELOW copy and pasted from the Learn Python 3: Functions Cheatsheet. My question is, why is the <year_to_check> variable step necessary? Can’t I just plug in <2018> directly into the <(year)> parameter in the function <def check_leap_year(year):>?

def check_leap_year(year):
if year % 4 == 0:
return str(year) + " is a leap year."
return str(year) + " is not a leap year."

year_to_check = 2018
returned_value = check_leap_year(year_to_check)
print(returned_value) # 2018 is not a leap year

ANOTHER EXAMPLE BELOW is from https://www.codecademy.com/courses/learn-python-3/lessons/intro-to-functions/exercises/multiple-returns. Why is the <monday, tuesday, wednesday> variable step necessary? I feel like this is yet another extra step. When calling the <threeday_weather_report> function, couldn’t I just simply code with the original variable names <first_day, second_day, third_day = threeday_weather_report>? Why make up entirely different names (monday, tuesday, wednesday)? …

weather_data = [‘Sunny’, ‘Sunny’, ‘Cloudy’, ‘Raining’, ‘Snowing’]

def threeday_weather_report(weather):
first_day = " Tomorrow the weather will be " + weather[0]
second_day = " The following day it will be " + weather[1]
third_day = " Two days from now it will be " + weather[2]
return first_day, second_day, third_day

monday, tuesday, wednesday = threeday_weather_report(weather_data)



I have “returned” with another Return question! This time from the link: https://www.codecademy.com/courses/learn-python-3/lessons/intro-to-functions/exercises/multiple-returns

I don’t understand the point of the return function. What’s wrong with my code below? I guess the holdup for me is that I don’t understand why I need to Return the variables first, second, and third? They didn’t go anywhere. They’re right there. So why can’t I just print each individual one out? The code that I attempted brought up a NameError saying that first wasn’t defined. But isn’t it defined inside the function as equal to “Rome”? …

def top_tourist_locations_italy():
first = “Rome”
second = “Venice”
third = “Florence”


Those three lines are in function scope.

Those three lines are in global scope. The variables are undefined in global scope, but can be defined as the expected return values of the function.

Inside the function we can return multiple values as a comma separated expression:

return first, second, third

and outside of the function, we can set the same variable names without any conflict owing to the different scope.

first, second, third = top_tourist_locations_italy()

The return values will be assigned in the order they are returned and do not depend on the variable names being the same. Technically we could have written,

a, b c = top_tourist_locations_italy()

with equal effect. The key is to have the same number of receiving variables as return values in the function.

Thank you mtf for your explanation. The global scope concept is new to me. I will practice more to get comfortable with Returns.

1 Like

So basically we have a function that return

def top_tourist_locations_italy(): first = "Rome" second = "Venice" third = "Florence" return first, second, third print(top_tourist_locations_italy())

multiple values which is the same as returning a Touple, right?

1 Like

Yes, multiple values separated by commas are returned as a tuple.

Output: <class 'tuple'>

From documentation of tuples:

Tuples may be constructed in a number of ways:

  • Using a pair of parentheses to denote the empty tuple: ()
  • Using a trailing comma for a singleton tuple: a, or (a,)
  • Separating items with commas: a, b, c or (a, b, c)
  • Using the tuple() built-in: tuple() or tuple(iterable)

    Note that it is actually the comma which makes a tuple, not the parentheses. The parentheses are optional, except in the empty tuple case, or when they are needed to avoid syntactic ambiguity. For example, f(a, b, c) is a function call with three arguments, while f((a, b, c)) is a function call with a 3-tuple as the sole argument.

Hey web7081998619!

It seems to me that Python, likewise other programming languages as JavaScript, uses a Zero Based paradigm to reference elements inside a list. Both languages also use Bracket Notation.

Let me try to explain both things to you.

When we have an array (a.k.a. a list), it’s commom the need to mention one or more of its elements.

If the array is:

array = [alpha, beta, gamma]

You may need to refer only to the second element (beta). How to do that?

First, you may use Bracket Notation, which means: name_of_the_array[position_of_element]

In this case (I’ll use the print function to help visualize it):


The console will print the word beta. So, array[1] works as a variable that stores the value “beta”.

So, you might question: why didn’t it print the word “alpha” instead, if the position of alpha is 1st, and the position of beta is 2nd?

The answer is: because Python is Zero Based. That means that the first position in a list is represented by the number 0, instead of 1.


array[0] equals alpha
array[1] equals beta
array[2] equals gamma

I believe understanding this exercise would have been easier had we been taught about arrays and how they work!

So the code on the left is mine, but i got a syntax error. the code on the right is the example code. they are identical but when i hit “replace with solution” the code worked. i think there is a problem in the course.

Check the indentation on line 6 where you assign the results of the function call to the most_popular variables.

1 Like