Can anyone explain this as simply as possible ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
28th Jan 2018, 9:17 PM
Nicolas Poulain
Nicolas Poulain - avatar
3 Answers
+ 5
A function assigns a new value to L list inside it (not the one in the global scope) and simply returns it -- assigns the ultimate value to the function itself, which you print out. B function, on the other hand, assumes the argument as reference and alters its first element, so the list L is in fact changed.
28th Jan 2018, 9:31 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 2
What is happening is 2 functions have been set, one to make the variable equal to the character A and the other to make the variable a list and set its first index as the character B. The first line sends the variable L through the first function. This will set L to A and return its value (therefore printing A). The second line will print the base value of L, before it went through any of the functions. The next line runs L through the second function. While in the function, the first index of L will be set to B, and it will return the entire list (except it will just be B) The final line will once again print the value of L, but because its first index was set to B instead of 1, it will print out ["B"]. Hope this helped!
28th Jan 2018, 9:29 PM
Faisal
Faisal - avatar
+ 1
Thank you !
28th Jan 2018, 11:11 PM
Nicolas Poulain
Nicolas Poulain - avatar