What is the output of the following? 1. a = 5 b = a a = 7 print(b) 2. a = [5] b = a a[0] = 7 print(b) Explain the output.
Answers
Answered by
2
I. a = 5 # a is initially 5
b = a # b becomes a which is 5
a = 7 # Now, a becomes 7
print(b) # b is printed which is 5
Hence, after execution a = 7 and b = 5.
II. a = [5] # a is initially a list with a single value 5
b = a # b now refers to the same list containing 5 in a
a[0] = 7 # Now, if a changes the value of the list then, it is reflected in b too as they both refer to the same list
print(b) # b is printed which is [7]
Hence, after execution a = b = [7].
Similar questions