+ 1
HOW DOES THIS CODE WORK? Could anyone illustrate my doubt?
arr=[1,2] def fun(n,a): if (a is not arr): a.append(n) return list(a) a=arr a=fun(3,a) print(a) a=fun(4,a) print(a)
13 Réponses
+ 2
Slick it works
+ 2
1) a = arr -> a and arr same object
2)a = fun(3,a)
if statement is false,no append
create new list/arr same as a, return and assign it to variable a
note that a and arr now are different object
3)print(a) still [1,2]
4)fun(4,a)
if statement now true
append num 4 to a and assign it to var a
5)print (a) = [1,2,4]
+ 1
You execute this code in sololearn python interpreter
0
Just execute this code in python interpreter
0
Abhay ill take your word for it. Pycharm doesnt like it
0
hi
0
আমি প্রোগ্রাম কিখতে চাই কেও কি সাহায্য করবেন..?? প্লিজ
0
Could you type in English to explain.
0
no
0
What country are you from, brother?
0
plzzz
- 1
It shouldnt. arr is not defined in the function
- 1
Just tried it, doesnt work.
Try:
if n not in a:
for the if statement