Difference between low, high and low_limit, high_limit?

On this exercise when we save the low_limit and high_limit as variables we only have to put them in as low and high? My question is do we not have to write out the entire name of low_limit and high_limit? Also can this be used to recall other variables as well where you can leave out the second word?

4 Likes

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

I don’t know why it is wrong

1 Like

@yometa

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

2 Likes

I had the same question, so I changed the names from low and high to x and y, and it worked anyway. So no, it’s not about being able to shorten the name of the variable. I don’t understand how it knows what I’m trying to store in low/x and high/y, though. Is it associating low with the first returned value and high with the second?

5 Likes

I’m perplexed by this as well. Up until this point we had to clearly define variables, at this stage somehow the low, high are taking on new values without a logical connection (that I can tell at least).

14 Likes

You could write out the whole name i.e. low_limit if you’d like but I think just to keep it simple, they used just low and high. Since we already put in calculations for the def get_boundaries. You could also write low_limit if you’d like… the calculation will still work!

2 Likes

I was also a little bit confused by that. Actually, I suppose that what made it more difficult for me to fully clarify the multiple return values context at the specific exersice with boundaries, was the repeating use of the words “low” and “high” in the code.
To keep its simplier and more clear , I tried to use letters. At least for me, this way quite helpful .The output was of course the same. For example:

def get_boundaries(target, margin):
L = target - margin
H = target + margin
return L, H

A,B = get_boundaries(100, 20)

#we added this print statement just to sanity-check our solution:
print("Low limit: “+str(A)+”, High limit: "+str(B))

In fact, by “A” I name the first call of the function which corresponds to the first return value “L” i.e. the first calculation (subtraction).
By “B” I name the second call of the function which corresponds to the second return value “H” i.e. the second calculation (addition).

23 Likes

I want to chime in: When the function returns variable1, variable2
Then when you call the function, setting it like this:
my_var1, my_var2 = function(input1, input2)
will ultimately give 2 variables my_var1 and my_var2 that are respectively equal to the output from the function as what variable1 and variable2 were.
In this example. low is equal to low_limit from when we ran the function with the (100, 20) input.(and same for high)
This is valuable because you could run the function again with a different input and set it to new variables:
low2, high2 = get_boundaries(50, 15) As an example.

27 Likes

I know I’m a little late to the party, but I was stuck on the same thing! After messing around with it a little bit, I figured it out. This is what my code looked like:

def get_boundaries(target, margin):
low_limit= target - margin
high_limit= margin + target
return low_limit, high_limit
low_limit, high_limit = get_boundaries (100, 20)
print(low_limit)
print(high_limit)
low = 80
high = 120

And it cleared! Seems like you just have to define low and high as the answers you got from the code.

4 Likes

If I understand this properly, there is no direct link between the strings ‘low’ and ‘low_limit’, and ‘high’ and ‘high_limit’.

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

Based on the above, the function get_boundaries() returns two variables; low_limit and high_limit, in that order. If we called the function using the syntax get_boundaries(100, 20), then the function would first return low_limit as 80 then high_limit as 120.

When the function get_boundaries() is preceded by *low, high = *, we’re technically creating two new variables; low and high. These variables are defined by their position, i.e. low is equal to low_limit because low is written first, and low_limit is the first variable returned by the function get_boundaries(). high is equal to high_limit as high is in the second position and high_limit is the second variable returned by the statement.

As a few people have suggested above, we could use any words in place of ‘low’ or ‘high’ to create these new variables - ‘a’ and ‘b’, ‘left’ and ‘right’, etc., as there isn’t as direct link between the terms ‘low’ and ‘low_limit’. It’s not what was used that mattered here, rather how it was used.

Hope that makes sense…

31 Likes

Blockquote

This is the way I was seeing it. If you were to create one variable and assign it the results you will get the readout of both returns (80, 120). I broke the variables low and high up by containing them on their own line and got a print out of (80, 120) (80, 120).

1 Like

This is what i did as well and it´s the best way i think to understand how it works.

It´s a bit confusing as it lacks some better explanation on the lesson

3 Likes

It doesn’t matter what you call them, the new variables would be assigned to low_limit and high_limit in the order they are listed. For instance, if you write:

high, low = get_boundaries(100, 20)

then high becomes low_limit and low becomes high_limit.

New variable names are created outside the function so that whoever is reading the code would be able to keep track of what is being used where. If you name the new variables low_limit and high_limit you could easily get confused and not be sure if low_limit refers to the variable in the function or the one outside it.

4 Likes

Hello
the low_limit and high_limit variables inside the function is totally different from the two outside the function. So even if you type the same full form (low_limit instead of low and high_limit instead of high) outside , new variables are created in the same name.
The variables defined inside a function works only inside (local scope) you can understand this by printing a variable inside a function.

def get_boundaries(target,margin):
  low_limit=target -margin
  high_limit = margin + target
  return low_limit,high_limit
low,high=get_boundaries(100,20)
print (low_limit) #will get error showing variable not defined
def get_boundaries(target,margin):
  low_limit = target - margin
  high_limit = margin + target
  return low_limit,high_limit
 low_limit,high_limit = get_boundaries(100,20) """ these variables are different from the ones defined inside. (totally new memory spaces with same name) """
print (low_limit)

correct me if i am wrong.

5 Likes

What we are doing here is defining the function below.

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

low, high =get_boundaries(100,20)

print(low) will now return 80 & print (high) returns 120. This is because inside of the variable “low,high” it is defined to use the Get_Boundaries function (Capitalized for ease of reading) Therefore it will relate back to that function and use the inputs we have chosen. 100 & 20 in this case

Low_Limit is a variable inside the function. We just need to use the function outside of the actual function and define it as Low or High. As seen in the above EX: low, high =get_boundaries(100,20)

3 Likes

Just realized that you can define mulptiple variables with a comma, I thought I was cheating when I did it. This is making me fall in love with python.

2 Likes

I know I am way late. As I have seen and learn from the the sections that clarify parameters (and correct me if I am wrong) this shows elements low_limit & high_limit and low and high are the same because of positional parameters given by the def get_boundaries (target, margin) function. For example, when you summon the ’ low, high = get_boundaries (100,20) ’ it is just using the positions of the parameters set (target, margin) which equal (100,20), and this essential means that low and high equal the positions of the next set of parameter given in the function. It can get confusing, but I believe it all relates to how your code is positioned either as first, second or third, in the function. If anything, I learned something new.

1 Like

Why do we need to redefine something we’ve already defined?

We’ve defined low_limit and high_limit with equations and then when we tell the program get_boundaries(100, 20) this should tell what each key word in the equation means. Perhaps I take the ease with which computer do things normal for granted but having to introduce low and high seems to be an unnecessary step logically speaking.

The variables that are defined inside the function are local only to that function. They cannot be seen. We need to return their values to see those.

def foo(m, n):
    return m - n, m + n

a, b = foo(100, 20)

What we are doing is unpacking a sequence. The return value contains multiple values, so it is packaged as a sequence (a tuple, probably).

1 Like

I would say it’s more to keep the code clear and simple to understand as if you use the same variable from the function like

Blockquote
print(low_limit)#would give you error to define the variable since you did define it as low
print(high_limit)
Blockquote

more or less keeping thing clear and consistent to understand when you review code later.