8
8
8
#1-WAP to create a dictionary to input your friends' names and their phone
numbers
n = int(input("Enter how many friends:"))
fd={}
for i in range(n):
name=input("Enter the name of your friend:") #key
ph =int(input("Enter the phone number number:")) #value
fd[name] = ph #creating a dictionary
print("Dictionary created")
print(fd)
1
# Program 36
#WAP to create a dictionary "Games"
#Team name - key , Number of wins and losses - values -list
n = int(input("Enter how many teams:"))
games={}
for i in range(n):
teamname=input("Enter the name of the team:") #key
w =int(input("Enter the number of wins:")) #value
l = int(input("Enter the number of losses:"))
games[teamname] = [w,l] #creating a dictionary
print("Dictionary created")
print(games)
print()
#WAP to accept the teamname and delete the particular team from the dictionary
print("To delete the particular team from the dictionary")
teamname=input("Enter the name of the team to be deleted:")
print("Deleted key-value pairs value=",games.pop(teamname))
#To delete the last item from the dictionary
print("Deleted key-value pairs is ",games.popitem())
print()
#WAP to modify the number of wins and losses for a particular team
print("To modify the number of wins and losses for a particular team")
teamname=input("Enter the name of the team for which the data to be modified:") #key
w =int(input("Enter the number of wins:")) #value
l = int(input("Enter the number of losses:"))#value
games[teamname] = [w,l]
print()
# Dictionary whose keys are month name and whose values are no. of days in the corresponding
month:
#) Ask the user to enter the month name and use the dictionary to tell how many days are in month.
month = { "jan" : 31 , "feb" : 28 , "march" : 31 , "april" : 30 , "may" : 31 , "june" : 30 ,\
"july" : 31 , "aug" : 31 , "sept" : 30 , "oct" : 31 , "nov" : 30 , "dec" : 31}
4
5
#Program 38
#WAP to accept n numbers from the user make it as a tuple. Find out the minimum and the
#maximum value and avg of those numbers
n = int(input("Enter how many numbers:"))
num=[]
for i in range(n):
x = float(input("Enter the number:"))
num.append(x)
m = tuple(num)
print("created tuple =",m)
print("Maximum value=",max(m))
print("Minimum value=",min(m))
s = sum(m)
avg = s/n
print("Average=",avg)
6
#Program 39
7
#Program 40
#To create a nested tuple
t =()
n=int(input("Enter the number of students"))
for i in range(n):
x =eval(input("Enterrno,name,marks"))
r = int(input("Rollno"))
name = input("Name")
marks=float(input("Marks"))
x=(r,name,marks) # creating a tuple
t = t + (x,) #concatenating as the tuple
print(t)
8
# PROGRAM 41
#WAP to create a tuple
9
# PROGRAM 42
#WAP to create nested tuple - name and 3 subject marks- for 3 student
st =()#empty tuple
for i in range(3):
name = input("Enter name:")
m1 = int(input("Enter m1:"))
m2 = int(input("Enter m2:"))
m3 = int(input("Enter m3:"))
x =(name,m1,m2,m3)
t = t + (x,)
print(t)
#Having created find out the sum of the marks of every student
for i in t: # tuple
sum = 0
sum = sum+i[1]+i[2]+i[3]
print("Total=",sum)
print("Average=",sum/3)
10
# Program 43
# Shifting all the zeros to right and all non-zeros number to left
lst=eval(input("Enter the list"))
length=len(lst)
end=length-1
print("Original list : ", lst)
i=0
while (i <= end): # traversing all the element
ele =lst[i]
if ele ==0:
for j in range(i,end): # iterating from i to end
lst[j]=lst[j+1] # shifting the element forward
else:
lst[end]=0 # at the end of list adding 0
end-=1
else:
i+=1 # increasing the value i
print("Zero shifted : ", lst)
11
# Program 44
# Enter two list print "Overlapped " if atleast one element in common
listA=eval(input("Enter a list:"))
listB=eval(input("Entera a list:"))
len1= len(listA)
len2= len(listB)
for a in range (len1):
ele=listA[a]
if ele in listB:
print("Overlapped")
break
else:
print("Separated")
12
#Program 45
# Find the second largest number from a given list
lst = eval(input("Enter list :"))
length = len(lst)
biggest = secondbiggest = lst[0]
for i in range (1, length):
if lst[i] > biggest:
secondbiggest = biggest
biggest = lst[i]
elif lst[i] > secondbiggest:
secondbiggest = lst[i]
print("Largest number of the list :", secondbiggest)
13
# Program 46
# Enter a element and a list remove all the occurrence of the element
lst=eval(input("Enter a list:"))
item=int(input("Enter the element to be removed:"))
c=lst.count(item)
if c==0:
print(item,"not in the list")
else:
while c>0:
i=lst.index(item)
lst.pop(i)
c=c-1
print("List after removing ",item,":",lst)
14