Why Does it work?


#1



https://www.codecademy.com/en/courses/python-beginner-en-qzsCL/1/1?curriculum_id=4f89dab3d788890003000096

Hi Guys, my question may be unorthodox, because i don't have an error message.

i typed in that code and it worked. But i really don't understand how that is possible.

the argument (numbers) is nowhere defined. So how is it that it can return the correct value?

i'm Sorry if the question is weird, but i never coded before codecademy, and i would like to understand rather than just following the Instructions.


lloyd = {
    "name": "Lloyd",
    "homework": [90.0, 97.0, 75.0, 92.0],
    "quizzes": [88.0, 40.0, 94.0],
    "tests": [75.0, 90.0]
}
alice = {
    "name": "Alice",
    "homework": [100.0, 92.0, 98.0, 100.0],
    "quizzes": [82.0, 83.0, 91.0],
    "tests": [89.0, 97.0]
}
tyler = {
    "name": "Tyler",
    "homework": [0.0, 87.0, 75.0, 22.0],
    "quizzes": [0.0, 75.0, 78.0],
    "tests": [100.0, 100.0]
}

# Add your function below!

def average(numbers):
    total = sum(numbers)
    total = float(total)
    total = total/len(numbers)
    return total


#2

function parameters (numbers in this case) serve as placeholder until you call the function:

def average(numbers):
    total = sum(numbers)
    total = float(total)
    total = total/len(numbers)
    return total

print average([1,2,3,4,5])

now the list gets copied into the function parameter


#3

To understand why we use numbers, you must first understand why we use parameters.

As of defining the function, the program does not know whether numbers is a number, a string, a list, a dictionary, etc. As far as the program is concerned numbers could be anything and, well, it could care less. It's just a placeholder for whatever you pass into the function when you actually call it. In other words think of parameters like variables. Whenever you call a function, the argument you pass into it becomes the value for numbers. For instance if you called:

average(tyler["homework"]);

You would essentially be telling the program to run average(numbers) except now, numbers = tyler["homework"]. Until a function is actually called, the parameters you have set are valueless placeholders. Once you call the function and pass in arguments for those parameters, they take on value and purpose. What I mean by this is, if you did the following:

lloyd = {
    "name": "Lloyd",
    "homework": [90.0, 97.0, 75.0, 92.0],
    "quizzes": [88.0, 40.0, 94.0],
    "tests": [75.0, 90.0]
}
alice = {
    "name": "Alice",
    "homework": [100.0, 92.0, 98.0, 100.0],
    "quizzes": [82.0, 83.0, 91.0],
    "tests": [89.0, 97.0]
}
tyler = {
    "name": "Tyler",
    "homework": [0.0, 87.0, 75.0, 22.0],
    "quizzes": [0.0, 75.0, 78.0],
    "tests": [100.0, 100.0]
}

def average(numbers):
    total = sum(numbers)
    total = float(total)
    total = total/len(numbers)
    return total

average(lloyd["homework"]);

Notice how I defined the function using numbers as a parameter and then I called the function using lloyd["homework"] as an argument. This is telling the program to run the code within average(numbers) but replace all mentions of numbers with lloyd["homework"]. Basically it does this:

total = sum(lloyd["homework"])
total = float(total)
total = total/len(lloyd["homework"])
return total

By the way, just so we are clear, lloyd["homework] is an identifier for the "homework" item in the dictionary known as lloyd.

This is why we use numbers as we do in the function.


#4

Thank you very much. That helped boosting my understanding by a lot:relaxed:


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.