How can Python understand that class_list refers to the 3 students if we haven't defined class_list before?


#1

How can Python use and understand some variables without any initialization?
For ex., in the exercise below, how can Python understand that class_list is a list containing the three lists “lloyd”, “alice”, “tyler”?

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 = float(sum(numbers))
  return total/len(numbers)

def get_average(student):
  homework = average(student["homework"])
  quizzes = average(student["quizzes"])
  tests = average(student["tests"])
  return homework * 10/100 + quizzes * 30/100 + tests * 60/100

def get_letter_grade(score):
  if score >= 90:
    return "A"
  elif score >= 80:
    return "B"
  elif score >= 70:
    return "C"
  elif score >= 60:
    return "D"
  else:
    return "F"
  
def get_class_average(class_list):
  results = []
  for student in class_list:
    results.append(get_average(student))
  return average(results)


#2

Hi @tagsolver66852,

When the get_class_average function is called, an argument representing a list will be placed inside parentheses following the function name. Then, as the function executes, class_list will refer to that list. For example, if this function call is made, class_list will refer to students as the function executes, assuming that the variable students has been defined …

print get_class_average(students)

Note that in Exercise 3:Put It Together, you were asked to create this statement …

students =  [lloyd, alice, tyler]

You should restore that statement because you will need it in a later exercise. Make sure that it is not indented, so that it is not included within any of the functions.


#3

But there lies my question again.

I have created the list students only in the exercise after this one,
and, nevertheless, in this exercise, my code, written exactly as I have posted it here above, ran without problems.

It is just a theoretical question (I have completed the unit already and I am not stuck with exercises):
how does Python understand that the argument inside the function refers exactly to the three students, if class_list has not been defined ?


#4

Hi @tagsolver66852,

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

As of defining the function, the program does not know whether class_list is a number, a string, a list, a dictionary, etc. As far as the program is concerned class_list 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 class_list. For instance if you called:

get_class_average([lloyd, alice, tyler]);

You would essentially be telling the program to run get_class_average(class_list) except now, class_list = [lloyd, alice, tyler]. 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 = float(sum(numbers))
    return total/len(numbers)

def get_average(student):
    homework = average(student["homework"])
    quizzes = average(student["quizzes"])
    tests = average(student["tests"])
    return homework * 10/100 + quizzes * 30/100 + tests * 60/100

def get_letter_grade(score):
    if score >= 90:
        return "A"
    elif score >= 80:
        return "B"
    elif score >= 70:
        return "C"
    elif score >= 60:
        return "D"
    else:
        return "F"
  
def get_class_average(class_list):
    results = []
    for student in class_list:
        results.append(get_average(student))
    return average(results)

get_class_average([lloyd, alice, tyler]);

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

results = []
for student in [lloyd, alice, tyler]:
    results.append(get_average(student))
return average(results)

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

You haven’t called the function yet, you have merely defined it. As I previously said,

The code within a function only runs if you call it. Without a function call, the program just ignores the function definition. The reason why you pass the exercise is because Codecademy’s SCT has confirmed that all the requirements have been met and that your function would work as intended if you were to call it.

“If a tree falls in the forest and you’re not there to hear it, it still makes a sound.”

“If a defined function works and you don’t call it, it still works.”


#5

Thank you! Now I have also understood why in for loops we can put anything we want (e.g., for animal/item/student/x in whatever: etc…).
I was missing the keyword “placeholder”, now I got it!
Thank you!


#6

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