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”?
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 …
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 ?
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:
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]:
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.”
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!