Q. Create a dictionary whose keys are month name and whose values are number of days in the corresponding month:
(a) Ask the user to enter the month name and use the dictionary to tell how many days are in month.
(b) Print out all of the keys in alphabetical order.
(c) Print out all of the month with 31 days.
(d) Print out the (key - value) pair sorted by the number of the days in each month.
You can understand by Watching video :-
Answer :-
(i)
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 ])
(ii)
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} lst = list ( month . keys() ) lst.sort() print( lst )
(iii)
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} print( "Month which have 31 days !!-- ") for i in month : if month [ i ] == 31 : print( i )
(iv)
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} print("Month according to number of days ---") print("feb") for i in month : if month [ i ] == 30 : print(i) for i in month : if month [ i ] == 31 : print( i)
Output :-
(i)
Enter the month name in short form :- feb
Number of days in feb = 28
>>>
Enter the month name in short form :- aug
Number of days in aug = 31
>>>
(ii)
['april', 'aug', 'dec', 'feb', 'jan', 'july', 'june', 'march', 'may', 'nov', 'oct', 'sept']
>>>
(iii)
Month which have 31 days !!--
jan
march
may
july
aug
oct
dec
>>>
(iv)
Month according to number of days ---
feb
april
june
sept
nov
jan
march
may
july
aug
oct
dec
>>>
year={"Jan":31,"Feb":28,"Mar":31,"Apr":30,"May":31,"Jun":30,"Jul":31,"Aug":31,"Sept":30,"Oct":31,"Nov":30,"Dec":31}
ReplyDeletename=input("Enter the month name in short form:")
if name.capitalize() in year:
print("(a).No. of days in the month of",name,"is:",year[name.capitalize()],"days")
else:
print("Invalid month name")
print()
print("(b).Months sorted in alphabetical order are:\n",sorted(year))
print()
months=[]
for i in year:
if year[i]==31:
months.append(i)
print("(c).Months having 31 days are:\n",months)
print()
lst1=[]
lst2=[]
count=30
for i in year:
if year[i]<=count:
lst1.append((i,year[i]))
else:
lst2.append((i,year[i]))
print("(d).(key-value) pairs sorted by the number of days in each months are:\n",lst1+lst2)
d = { "jan" : 31 , "feb" : 28 , "march" : 31 , "april" : 30 , "may" : 31 , "june" : 30 ,
ReplyDelete"july" : 31 , "aug" : 31 , "sept" : 30 , "oct" : 31 , "nov" : 30 , "dec" : 31}
d1= { }
l=[]
l1=[]
d1[28]="feb"
print("Month according to number of days ---")
print("feb")
for i in d :
if d [ i ] == 30 :
l.append(i)
d1[d[i]]=l
elif d [ i ] == 31 :
l1.append(i)
d1[d[i]]=l1
for i in (d1.items()):
print(i[0],i[1])
Correct.
Deleted = { "jan" : 31 , "feb" : 28 , "march" : 31 , "april" : 30 , "may" : 31 , "june" : 30 ,
ReplyDelete"july" : 31 , "aug" : 31 , "sept" : 30 , "oct" : 31 , "nov" : 30 , "dec" : 31}
d1= { }
l=[]
l1=[]
d1[28]="feb"
print("Month according to number of days ---")
for i in d :
if d [ i ] == 30 :
l.append(i)
d1[d[i]]=l
elif d [ i ] == 31 :
l1.append(i)
d1[d[i]]=l1
for i in (d1.items()):
print(i[0],i[1])
Correct.
DeleteNice
ReplyDeletePost a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )