Tuesday, November 19, 2019

insert method in python

l=[]
l.append(10)
l.append(20)
l.append(30)
l.append(40)
print(l)
l.insert(1,50)
print(l)

output:
[10, 20, 30, 40]
[10, 50, 20, 30, 40]

example 2:

l=[]
l.append(10)
l.append(20)
l.append(30)
l.append(40)
print(l)
l.insert(1,50)
print(l)
l.insert(50,888)
print(l)
l.insert(-20,333)
print(l)

output:
[10, 20, 30, 40]
[10, 50, 20, 30, 40]
[10, 50, 20, 30, 40, 888]
[333, 10, 50, 20, 30, 40, 888]

l=[]
l.append(10)
l.append(20)
l.append(30)
l.append(40)
print(l)
l.insert(1,50)
print(l)
l.insert(50,888)
print(l)
l.insert(-20,333)
print(l)
print(l.index(888))
print(l.index(333))

output:
[10, 20, 30, 40]
[10, 50, 20, 30, 40]
[10, 50, 20, 30, 40, 888]
[333, 10, 50, 20, 30, 40, 888]
6
0



No comments:

Post a Comment

python class topic video