0

What is the output and why?

a=[1,2,3,4] b=a a[2]=5 print(b)

7th Sep 2018, 9:45 AM
Akshay Khatri
Akshay Khatri - avatar
3 Answers
+ 1
That's because in Python the assignment points to same object. So b is just another name for the object, which was called a. If you want b to be another object, you need to modify the line b=a to b = a.copy() Then if you change an element in a, it's not affecting b
7th Sep 2018, 10:13 AM
Matthias
Matthias - avatar
0
The output is [1,2,5,4] a=[1,2,3,4] //here a[2]=3 b=a a[2]=5 //Here replacing the value a[2]=3 to a[2]=5 print(b)//so the output is [1,2,5,4]
7th Sep 2018, 10:03 AM
viji
0
but we are printing b not a?
7th Sep 2018, 10:12 AM
Akshay Khatri
Akshay Khatri - avatar