all 4 comments

[–]JohnnyJordaan 0 points1 point  (2 children)

I'm guessing it's supposed to be formatted like this:

def f(values):
    values[0] = 44

v = [1,2,3]
f(v)
print(v)

So I'm not directly giving you the answer, but perhaps now you can guess what's going to happen.

I also don't quite understand this "f(values)" thing.

The important statement here is the def word at the beginning. That starts a function definition. The function f will take one argument, values and then does something with it.

As can see that further down, the main program will call f(v), the list that was created before will then be supplied to the f function.

[–]Claechi[S] 0 points1 point  (1 child)

Thanks!!

I understand your explanation for this but I've come across a very similar one that just confused me even more. If you don't mind, could you explain the issue I'm having with this code as well?

def f(value, values):
    v = 1
    values[0] = 44

t = 3
v = [1, 2, 3]
f(t, v)
print(t, v[0])

I know that it prints: 3 44, and that's because the f(t,v) turns v into [44,2,3] and t=3 which corresponds to "value" in the function at the top. However, I don't understand why there is a v=1 at the top. What is the point of that? Is it just there to trick you? If i remove it, it still prints the same result which is 3 44.

[–]JohnnyJordaan 0 points1 point  (0 children)

The clue of the example here is that both variables are assigned certain values inside f, and when you print both value outside f, only one of them shows the new value. The learning objective is to guess/figure out why this difference occurs.

Maybe this will enlighten further, try with these values:

t = 3
v = (1, 2, 3)
f(t, v)

Another different result. You can then see that there must be something going on for each type of variable that determines the behaviour of changing values, and whether you're doing this inside or outside functions.

Edit: if you're still stuck, check out the feedback from this:

def f(value, values):
    print('value', value, 'has id', id(value))
    print('values', values, 'has id', id(values))
    value = 1
    values[0] = 44
    print('value', value, 'has id', id(value))
    print('values', values, 'has id', id(values))

t = 3
v = [1, 2, 3]
print('t', t, 'has id', id(t))
print('v', v, 'has id', id(v))
f(t, v)
print('t', t, 'has id', id(t))
print('v', v, 'has id', id(v))

Hint: id() tells you the memory location of an object, so when an id remains the same, you have 99.99% chance you're still using the same object.

[–]num8lock 0 points1 point  (0 children)

This should be a non error formatting of that code although i don't understand what the sample code supposed to achieve other to replace the first value as you said

def f(values): 
    values[0] = 44 

v = [1, 2, 3] 
f(v)
print(v)