Modifying an element of a list in a function


#1



https://www.codecademy.com/en/courses/python-beginner-nzzVa/2/3?curriculum_id=4f89dab3d788890003000096


Hello. I'm having a hard time understanding functions. I got the solution with the code below, but I am confused as to how this function can take the argument (n) when the def list_function takes the argument (x). Basically I guessed to get the solution. First I tried this on (n) and it was incorrect, then I tried it on (x) and it was correct. I'm just seeking some clarification from anyone who might have more knowledge on functions than I do.

Thanks


 def list_function(x):
    x[1] = x[1] + 3
    return x

n = [3, 5, 7]
print list_function(n)


#2

def function(args):
    # code that uses local variable, `args`

print function(arguments)    # the function call passing arguments to the function

Call arguments can be any form, literal or variable. When the arguments are received by the function they are copied into the local variable(s) defined as the parameter(s).

Inside the function, x is the name of the local variable, but it refers to the list n that is passed in.


#3

Awesome. This was what I suspected, I was just unsure. This helps.

Thank you


#4

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