+ 3
Can someone explain the solution for below question ?
What is the output of below code ? a=[1,[2,3]] b=a[:] a[1][1] = 5 a[0] = 3 print[b] I was expecting [3,[2,5]] as the output. But the actual answer is [1,[2,5]].
10 Answers
+ 5
b creates a deep copy of 'a', except a[1], where the reference is the one copied. Therefore, affecting a[1] affects b[1] while it does not apply for a[0] and b[0]
+ 5
https://realpython.com/copying-JUMP_LINK__&&__python__&&__JUMP_LINK-objects/
This one explains the different types of copies.
+ 4
What is the output of this code?
a = {1, 2, 3}
b = {0, 3, 4, 5}
print(a & b)
Ans: print a & b i.e the number which are present in both set a and set b
output: 3
+ 3
Thank you for answering 😀
Can you suggest me some site for refernce or please provide in detail about that except you mentioned. Thank you in advance
+ 3
a=[1,[2,3]]
b=a[:]
a[1][1] = 5
b[0] = 3
print(b)
hey Rajesh, at second last line you are assigning a new value to a' instead assign it to b
+ 3
The b wont change until it got declared.
a = 10
b = a*2 #b is 20
a = 20 #b is still 20
maybe, this helped.👀🔥
+ 1
Thank you promethesus and Deepak.
0
What is the output of this code? a = {1, 2, 3} b = {0, 3, 4, 5} print(a & b)
output:3
0
answer is 3
0
the answer is {3} and not just 3