0

Why the output is different from my answer? [Solved]

arr = [1,2] def func(n,a): if(a is not arr): a.append(n) return list(a) a = arr a = func(3,a) print(a) Output - [1,2] Why not - [1,2,3]

13th Feb 2021, 7:28 AM
Nalayak_
Nalayak_ - avatar
14 Respostas
+ 6
SauravK.__ Because outside the function the 'a' has changed after the first call. a = arr # <--- a is arr # When we called the function in this line, the 'a' still holds the reference of 'arr', but after we called the function, the 'a' now holds a different value. a = func(3, a) a = func(4, a) # <--- a is not arr a = func(5, a) # <--- a is not arr Notice that the value of 'a' is changing everytime we call the function and assigning the returned value to 'a',.
13th Feb 2021, 8:36 AM
noteve
noteve - avatar
+ 5
The condition "if (a is not arr)" inside the function is False and therefore it will not append 3 to `a`. `a` and `arr` has the same memory address as we explicitly declared `arr` to `a` in line 6, i.e. they are the same object. Remember that when assigning list to another variable, we don't pass its values, we pass its reference.
13th Feb 2021, 7:31 AM
noteve
noteve - avatar
+ 5
I'm not sure if I understand the 'check condition one by one'. Though I can say that yes it will check the condition when we call the function. First, we call the function on (2) and the variable `a` now holds the "new" returned list. And so on (2) the value of `a` has been changed and so the next time we call the function in (3), the condition will now become True. Remember that [1, 2] is not [1, 2] if they have different memory address. For example: x = [1, 2] y = [1, 2] print(x == y) # True print(x is y) # False
13th Feb 2021, 7:56 AM
noteve
noteve - avatar
+ 4
Yes it will, I did not notice that the variable that uses the function has the same variable 'a'. a = arr a = func(3, a) # <--- a is arr a = func(4, a) # <--- a is not arr because a is now a different [1, 2] returned by the function therefore the condition will become True and append 4. Things will be different if we use variables except 'a'. a = arr x = func(3, a) y = func(4, a) z = func(5, a) >> [1, 2] Because the value of 'a' did not change and it still stores the reference of 'arr'.
13th Feb 2021, 7:48 AM
noteve
noteve - avatar
+ 4
SauravK.__ Before the first function call 'a' was arr so if you check (a is not arr) it will return False and not append value but now returned value's reference is changed so after 1st function call if you again check (a is not arr) then it will return True and append 4 in 'a' If you assign arr to 'a' after 1st function call then 4 will not be append in 'a' You can try this arr = [1, 2] def func(n, a): if(a is not arr): a.append(n) return list(a) a = arr a = func(3, a) print (a) a = arr #assigned again. #try to remove the above line and see the effect a = func(4, a) print(a)
13th Feb 2021, 9:15 AM
AÍąJ
AÍąJ - avatar
+ 3
SauravK.__ Question is correct but problem is in if condition. if (a is not arr) here this condition will be false because a is a arr because you have assigned arr to a and passed a in function so reference of a and arr will be same.
13th Feb 2021, 7:58 AM
AÍąJ
AÍąJ - avatar
+ 2
Cyan Finally ! doubt cleared Thank you both of u in short : a = arr --> a is arr a = func(3,a) = arr --> a is arr a = func(4,a) where a = func(3,a)=arr a!=func(4,a) also a is not func(4,a) --> a is not arr now if statement is correct & it will append n after a
13th Feb 2021, 9:10 AM
Nalayak_
Nalayak_ - avatar
+ 1
Cyan what will happen in case if we add " a = fun(4,a) " after 7th line?
13th Feb 2021, 7:39 AM
Nalayak_
Nalayak_ - avatar
+ 1
Cyan I also felt the same but when I tried this in code editor then output comes : [1,2,4]
13th Feb 2021, 7:45 AM
Nalayak_
Nalayak_ - avatar
+ 1
SauravK.__ There should be if (a is arr): a.append(n)
13th Feb 2021, 7:53 AM
AÍąJ
AÍąJ - avatar
0
Cyan you want to say that: first it will check condition one by one? 1.a = arr 2.a = func(3,a) 3.a = func(4,a)
13th Feb 2021, 7:53 AM
Nalayak_
Nalayak_ - avatar
0
I Am AJ ! but I feel question is correct. dont you?
13th Feb 2021, 7:55 AM
Nalayak_
Nalayak_ - avatar
0
I didn't get your point clearly but I wrote whatever given in that question.. basically this was the question: arr = [1,2] def func(n,a): if(a is not arr): a.append(n) return list(a) a = arr a = func(3,a) a = func(4,a) print(a) I was stuck here I didn't understand that why the condition of if statement is true for a = func(4,a) but not for a = func(3,a)
13th Feb 2021, 8:12 AM
Nalayak_
Nalayak_ - avatar
0
I Am AJ ! Bhaiya this is same what I tried earlier ! output will be [1,2] as a = arr also a = func(3,a) so arr =func(3,a) --> a is arr..
13th Feb 2021, 9:22 AM
Nalayak_
Nalayak_ - avatar