0% found this document useful (0 votes)
4 views14 pages

8

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 14

# Program 35

#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()

#WAP to sort on names of the teams


print("To sort on names of the teams")
games1 = sorted(games.items())
print("Sorted dictionary")
print(games1)
print()

#To search for a particular team's wins and losses


print("To search for a particular team's wins and losses")
teamname = input("Enter the teamname:")
e=[]
for i in games: # i - take the keys
if i == teamname:
e=games[i]
print(e)
2
3
#Program 37

# 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}

mon = input("Enter the month name in short form :- ")


print("Number of days in ",mon,"=",month [ mon ])
print()

#Print out all of the keys in alphabetical order.


print("Prints out all of the keys in alphabetical order.")
lst = list ( month . keys() )
lst.sort()
print( lst )
print()
#Print out all of the month with 31 days.
print("Prints out all of the month with 31 days.")
print( "Month which have 31 days !!-- ")
for i in month :
if month [ i ] == 31 :
print( i )
print()
#Print out the (key - value) pair sorted by the number of the days in each month.
print("Print out the (key - value) pair sorted by the number of the days in each month.")
v=sorted([(i,j) for j,i in month.items()])
print([(j,i) for i , j in v])

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

#WAP to input names of n students


#store them in a tuple
#Input a name and find if this student is present in the tuple or not
name=[] # empty list
n = int(input("Enter how many names:"))
for i in range(n):
x = input("Enter the name:")
name.append(x)
m = tuple(name) #converting list into tuple
print("Created tuple of names",m)
search=input("Enter the name to be searched for:")
if search in m:
print("Yes the name is present")
else:
print("No the name is not in the tuple")

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy