Python-Lists.
ipynb - Colab 13/06/24, 3:58 PM
# List index
L = [11, 12, 13, 14, 12, 15, 16]
print([Link](12))
print([Link](12, 2))
print([Link](12, 4))
print([Link](12, 2, 6))
1
4
4
4
print(L[:])
# List slices
print(L[:-2])
L = [11, 12, 13, 14, 15, 16]
print(L[1:1])
print(L[1:])
print(L[:3])
print(L[:])
print(L[:-2])
print(L[:-1])
[]
[12, 13, 14, 15, 16]
[11, 12, 13]
[11, 12, 13, 14, 15, 16]
[11, 12, 13, 14]
[11, 12, 13, 14, 15]
L[1:3] = [2, 1]
L = [0, 1, 2, 3, 4, 5, 6]
L
L[1:3] = [2, 1]
L
[0, 2, 1, 3, 4, 5, 6]
L[1:3] = [1, 2, 3, 4, 5, 6]
L = [0, 1, 2, 3, 4, 5, 6]
L
L[1:3] = [1, 2, 3, 4, 5, 6]
L
[0, 1, 2, 3, 4, 5, 6, 3, 4, 5, 6]
[Link] Page 1 of 10
[Link] - Colab 13/06/24, 3:58 PM
L = [0, 1, 2, 3, 4, 5, 6]
L[1:3] = [11]
L
[0, 11, 3, 4, 5, 6]
L = [0, 1, 2, 3, 4, 5, 6]
L[0:1] = []
L
[1, 2, 3, 4, 5, 6]
L = [0, 1, 2, 3, 4, 5, 6]
L[5:] = []
L
[0, 1, 2, 3, 4]
L = [0, 1, 2, 3, 4, 5, 6]
L[:] = []
L
[]
L = [0, 1, 2, 3, 4, 5, 6]
[Link](7)
L
[0, 1, 2, 3, 4, 5, 6, 7]
L = [0, 1, 2, 3, 4, 5, 6]
L[len(L):] = [7]
L
[0, 1, 2, 3, 4, 5, 6, 7]
[Link] Page 2 of 10
[Link] - Colab 13/06/24, 3:58 PM
L1 = [0, 1, 2, 3]
L2 = [4, 5, 6, 7]
[Link](L2)
print(L1)
print(L2)
[0, 1, 2, 3, 4, 5, 6, 7]
[4, 5, 6, 7]
L1 = [0, 1, 2, 3]
L2 = [4, 5, 6, 7]
L1[len(L1):] = L2
print(L1)
print(L2)
[0, 1, 2, 3, 4, 5, 6, 7]
[4, 5, 6, 7]
L = [0, 1, 2, 3]
[Link](4, 4)
L
[0, 1, 2, 3, 4]
L = [0, 1, 2, 3]
[Link](14, 4)
L
[0, 1, 2, 3, 4]
L = [0, 1, 2, 3]
[Link](0, -1)
L
[-1, 0, 1, 2, 3]
[Link] Page 3 of 10
[Link] - Colab 13/06/24, 3:58 PM
L = [0, 1, 2, 3]
del L[1]
L
[0, 2, 3]
L = [0, 1, 2, 3]
del(L[1:])
L
[0]
L = [0, 1, 2, 3, 4, 5, 6]
del L[Link]
L
[0, 2, 4, 5, 6]
L = [0, 1, 2, 3, 4, 5, 6]
del(L[:])
L
[]
L = [0, 1, 2, 1, 3, 4, 5, 6]
[Link](1)
L
[0, 2, 1, 3, 4, 5, 6]
L = [0, 1, 2, 1, 3, 4, 5, 6]
[Link](0)
L
[1, 2, 1, 3, 4, 5, 6]
[Link] Page 4 of 10
[Link] - Colab 13/06/24, 3:58 PM
L = [0, 1, 2, 1, 3, 4, 5, 6]
[Link](-1)
L
[0, 1, 2, 1, 3, 4, 5]
L = [0, 1, 2, 1, 3, 4, 5, 6]
[Link]()
L
[0, 1, 2, 1, 3, 4, 5]
L = [0, 1, 2, 1, 3, 4, 5, 6]
[Link]()
L
[]
L1 = [0, 1, 2, 3]
L2 = [4, 5, 6, 7]
L = L1 + L2
L
[0, 1, 2, 3, 4, 5, 6, 7]
L = [0, 1, 2, 3]
L = L * 3
L
[0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3]
[Link] Page 5 of 10
[Link] - Colab 13/06/24, 3:58 PM
L1 = [0, 1, 2, 3]
L2 = L1
L1[0] = 10
print(L1)
print(L2)
[10, 1, 2, 3]
[10, 1, 2, 3]
L1 = [0, 1, 2, 3]
L2 = L1
L2[0] = 10
print(L1)
print(L2)
[10, 1, 2, 3]
[10, 1, 2, 3]
L1 = [0, 1, 2, 3]
L2 = [Link]()
L1[0] = 10
print(L1)
print(L2)
[10, 1, 2, 3]
[0, 1, 2, 3]
L1 = [0, 1, 2, 3]
L2 = [Link]()
L2[0] = 10
print(L1)
print(L2)
[0, 1, 2, 3]
[10, 1, 2, 3]
[Link] Page 6 of 10
[Link] - Colab 13/06/24, 3:58 PM
L = [0, 1, 2, 3]
print(min(L))
print(max(L))
0
3
L = [0, 1, 2, 3]
[Link]()
L
[3, 2, 1, 0]
L = [10, 21, 12, 31, 4, 7]
[Link]()
L
[4, 7, 10, 12, 21, 31]
L = [10, 21, 12, 31, 4, 7]
[Link](reverse=False)
L
[4, 7, 10, 12, 21, 31]
L = [10, 21, 12, 31, 4, 7]
[Link](reverse=True)
L
[31, 21, 12, 10, 7, 4]
[Link] Page 7 of 10
[Link] - Colab 13/06/24, 3:58 PM
# Program to store country names and print the names
# of those countries whose length is greater than 5.
countries = ["India","Pakistan","Sri Lanka","China"]
countries_GFive = []
for country in countries:
if len(country) > 5:
countries_GFive.append(country)
print(countries_GFive)
['Pakistan', 'Sri Lanka']
# Program to store country names and print the names
# of those countries whose length is greater than 5.
countries = ["India","Pakistan","Sri Lanka","China"]
for country in countries:
if len(country) > 5:
print(country)
Pakistan
Sri Lanka
countries = []
country = input("Enter country name (or 'end' to terminate):")
while country != "end":
country = input("Enter country name (or 'end' to terminate):")
[Link](country)
for country in countries:
if len(country) > 5:
print(country)
Enter country name (or 'end' to terminate):India
Enter country name (or 'end' to terminate):end
[Link] Page 8 of 10
[Link] - Colab 13/06/24, 3:58 PM
countries = []
while True:
country = input("Enter country name (or 'end' to terminate):")
if country == "end":
break
[Link](country)
for country in countries:
if len(country) > 5:
print(country)
Enter country name (or 'end' to terminate):India
Enter country name (or 'end' to terminate):China
Enter country name (or 'end' to terminate):Sri Lanka
Enter country name (or 'end' to terminate):United Kingdom
Enter country name (or 'end' to terminate):end
Sri Lanka
United Kingdom
# Program to accept country names and print their capitals
countries=["India","Pakistan","Sri Lanka","China"]
capitals=["New Delhi","Islamabad","Sri Jayawardenepura Kotte","Beijing"]
while True:
country = input("Enter country name (or 'end' to terminate):")
if country != "end":
country_index = [Link](country)
print(f"Captial of {country} is {capitals[country_index]}")
else:
break
Enter country name (or 'end' to terminate):India
Captial of India is New Delhi
Start coding or generate with AI.
[Link] Page 9 of 10
[Link] - Colab 13/06/24, 3:58 PM
[Link] Page 10 of 10