List Tuple and Sets in Python
#list
list1=['1','2','3','3','4','5']
list2=['a','b','c','d','e']
print(list1)
print(list2)
list1.append("6")
print(list1)
list1.remove("1")
print(list1)
x=list1.count("3")
print(x)
list1.pop()
print(list1)
list3=list1+list2
print(list3)
print(list3[2:5])
print(list3[-2])
print(list3[-4:-1])
list3[1]="apple"
print(list3)
for x in list3:
print(x)
if "apple" in list3:
print("yes, apple is present in list3")
print(len(list3))
list3.insert(2,"orange")
print(list3)
del list3[0]
print(list3)
#tuple
tuple1=("apple","banana","cherry","mango","mango","grapes","abc","def","ghi","ijk")
print(tuple1)
print(tuple1[2])
print(tuple1[2:5])
print(tuple1[-5:-1])
print(tuple1[2:])
print(tuple1[:4])
tup2=('1','2','3','4','5','6','7','8','9')
print(tup2)
print("length of tup2 is :",len(tup2))
if "2" in tup2:
print("yes 2 in tup2 ")
for y in tup2:
print(y)
tup3=tuple1+tup2
print(tup3)
c=tup3.count("mango")
print(c)
print("length of tup3 is :",len(tup3))
tup4=('2a','2b','2c',"2a","2a")
z=tup4.count("2a")
print(z)
#sets
set1={"app","data","run","output"}
print(set1)
for s in set1:
print(s)
if "app" in set1:
print("app is in set1")
print("data" in set1) #check if data is present in set1
set1.add("code") #add item in set
print(set1)
#to add more than one item in set
set1.update(["error","compiler","add","div"])
print(set1)
Comments
Post a Comment