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
>>>
Post a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )