What is the output of the following Python code b 2 3 4 c

What is the output of the following Python code? b = [2, 3, 4] c = [1 2, b, 5] d = c e = d [:] b.append(6) c. append(7) e [0] =0 print (b, \'\ \', c, \'\ \', d, \'\ \', e)

Solution

Step1 :

b = [2, 3, 4] # b contains [2, 3, 4]

Step2 :

c = [1, 2, b, 5] # c contains [1, 2, [2, 3, 4], 5]

Step3 :

d = c # c contains [1, 2, [2, 3, 4], 5]

Step4 :

e = d[:] # means create a shallow copy of d and assign it to e, e contains [1, 2, [2, 3, 4, 6], 5]

Step5 :

b.append(6) # Since we append 6, now b contains [2, 3, 4, 6] and c changed to [1, 2, [2, 3, 4, 6], 5], d

Step6 :

changed to [1, 2, [2, 3, 4, 6], 5] and e changed to [1, 2, [2, 3, 4, 6], 5]

Step7 :

c.append(7) # Since we append 7 c contains [1, 2, [2, 3, 4, 6], 5, 7] and d changed to [1, 2, [2, 3, 4, 6], 5, 7]

Step8 :

e[0] = 0 # since we replace first element with 0 now e changed to [0, 2, [2, 3, 4, 6], 5]

Step9 :
print(b, \'\ \', c, \'\ \', d, \'\ \', e)

([2, 3, 4, 6], \'\ \', [1, 2, [2, 3, 4, 6], 5, 7], \'\ \', [1, 2, [2, 3, 4, 6], 5, 7], \'\ \', [0, 2, [2, 3, 4, 6], 5])

Output :

([2, 3, 4, 6], \'\ \', [1, 2, [2, 3, 4, 6], 5, 7], \'\ \', [1, 2, [2, 3, 4, 6], 5, 7], \'\ \', [0, 2, [2, 3, 4, 6], 5])

 What is the output of the following Python code? b = [2, 3, 4] c = [1 2, b, 5] d = c e = d [:] b.append(6) c. append(7) e [0] =0 print (b, \'\ \', c, \'\ \', d

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site