FAQ: Introduction to Functions - Multiple Return Values

How can I make my variables low and high dynamic?

def get_boundaries(target, margin):
  low_limit = target - margin
  high_limit = margin + target

  return low_limit, high_limit

print(get_boundaries(100, 20))
low = 80
high = 120

By making them the return values. The function returns a tuple that you can unpack at the caller.

a, b = foo(u, v)

This is where I was confused- Save the returned values to variables called low and high .

Codecademy doesn’t give a clear explanation on how to save returned values to variables as you have explained with the example low, high = get_boundaries(target, main).

With using this example, I’m guessing that the computer assigns low and high the values of low_limit and high_limit based on the order in which they come by? since none is clearly defined when assigning the function.

1 Like

Yes, the order of the return values is fixed and they are assigned to the variables we supply in that same order. To see the complete package, assign it to a single variable and print it out.

>>> a = get_boundaries(100, 20)
>>> print a
(80, 120)

The tuple is the package.

>>> low, high = a
>>> low
>>> high
1 Like

this still doesnt make sense to me, why a “tuple” even exists?this is the problem with code academy as well, if you dont get it you got a forum and thats it, if you still dont get it , its o well. this is what scares me and makes me think im wasting my time, that sooner or later ■■■■ is gonna get real hectic with math and missing logical steps that have to be inferred, (like here). there was no such thing as a “tuple” in the previous lessons with multiple parameters multiple arguments, went real smooth everything had a clear step no hidden “tuples”. to me in the example “low” and “high” are syntax errors not previously defined or assigned; your asking the computer to infer?!. this one was troublin for me.

They are defined in the line where we assign them the return values from the function. The return sends back two values (a sequence), which correspond to the two variables in the assignment.

Python has four basic data structures (collections):

1. list   =>  []     =>  ordered, mutable, duplicates permitted
2. dict   =>  {k: v} =>  ordered, mutable, unique keys
3. tuple  =>  ()     =>  ordered, immutable, duplicates permitted
4. set    =>  {}     =>  not ordered, unique immutable values, mutable

Lists and tuples both support accessing by index. With lists we can mutate the values, add to the list, or remove items. Tuples can be accessed for read only; values cannot be changed, added or removed.

Dictionaries support accessing by keys; new key-value pairs can be added; key-value pairs can be removed, and values are mutable. When accessing keys that exist we can use dot notation. When accessing keys by reference (through a variable) we must use bracket notation (subscript).

Sets have no order and do not support accessing by index. We can add items or remove them but cannot change them.

Each of the above collections have scenarios where they are the best fit. There is a good deal of literature on each one. For instance, here is a decent article on sets…


reading your responses give me a headache. its like reading chinese or something. real hard core ,full nerd. this sure doesn’t come east to me. when i wrote that i was frustrated. i will read your reply in full later on and see what i can absorb.
so im at the project at the end of functions called “prepare for physics class”.its not coming easy, ive retained very little and need more repetitions. is there some place i could go to just right little examples of functions? should i continue on with python and worry about being fast and having it all committed to memory later? should i just redo the whole functions set and see how i feel then? 90% of makes sense once i see the correct answer, but the recall just isn’t there. truth be told i have not committed myself like i should, maybe a half hour a day at the most, some days ive just checked in and done nothing just to keep my streak going (which is good, right? its building a habit and staying with it) but my learning is suffering , i know. thanks for your time mtf!

This is a big deal, your diminishing commitment and enthusiasm. Truth is I don’t blame you if some of my explanations appear foreign, that is to be expected. I use a kind of middle language between beginner and intermediate as a default, without disrespect of the reader. It’s only until we can get a feel for the lay of the land, so to speak.

We don’t know where the learner is when their first question appears. A reply will not offer much at first because we need engagement, a handshake, if you would. Once there is a clear path of communication we can then drill down to the core difficulty with the awareness of the learner. When they admit their own weaknesses, we can begin to strengthen them with coaching and guidance.

All of this takes commitment. We are committed to every learner we engage. (When I say ‘we’, I mean every member who is fielding questions from the array of daily questions.)

The learner with little more than 30 minutes a day is going to fall behind very quickly. Any musician will tell you that it takes hours and hours to develop muscle memory. Concert musicians rehearse and practice for six hours a day to keep at the top of their abilities. At the very least one would hope a learner will commit a couple of hours a day, organized into lesson, review, reading, practice and drill.

We cannot learn programming by osmosis. Being around it isn’t going to teach us what we really need to learn, only a host of neat tricks that are soon forgotten. Learn to code, then write those neat little tricks on your own. It’s a different level of commitment, and it’s all on you. Don’t thank me for my time. Thank yourself in ten years when you make it on your own.

1 Like


1 Like

I am currently on: Learn Python 3 > Functions > 9. Return Multiple Values

I am aware that the correct code is:

def get_boundaries(target, margin):
low_limit = target - margin
high_limit = margin + target

return low_limit, high_limit

low, high = get_boundaries(100,20)


However, when I first tried out the code, I did it like this:

def get_boundaries(target, margin):
low_limit = target - margin
high_limit = margin + target

return low_limit
return high_limit

low, high = get_boundaries(100,20)

print (low)
print (high)

If I am correct, at the end you put a comma between low and high, essentially assigning the first return to low, and the second return to high. I am just confused as to why there must be a comma in the return value rather than having two separate return statements? What exactly is the computer going through that doesn’t allow for me to put in two separate return statements?

Once return is executed the value(s) specified are passed to the caller, in this case back to your main script, and your function at this point is finished. You can try this if you like by adding calculations or print statements or similar after the first return as none of them will be executed. Put simply return exits your function when it is run.

If you want a little more detail about the point of the comma then read on: Return can technically only return one object. What happens when you use the comma is you create a tuple and pass that back instead. If you’re unfamiliar with tuples then imagine it as list instead e.g. return [a, b] (the main difference being the tuple itself cannot be changed once created). It’s a single object but it contains references to multiple other ojbects.