-2

I wrote the following code in python. I was not able to understand the logic behind the output. Can you help me out?

a= [1,2,3]
print(a)
del a[1]
print(a)

The result came out to be [1, 2, 3] [1, 3]

joey lang
  • 187
  • 2
  • 8

1 Answers1

0

Okay, Ill try to be as comprehensive as I can

a= [1,2,3] is a list of integers

so print(a) will output the entire list [1,2,3]

now python has a way it counts items in a list with '0' denoting the first item.'1' the second item and so on. In you're case:

item 0 = 1
item 1 = 2
item 2 = 3
del a(1) = del '2'

the new list is:

print (a) = [1,3]
myrdstom
  • 156
  • 3
  • 15