Вы находитесь на странице: 1из 6
F le ee eee Se eee ooo ANSWERS TO FILL IN THE BLANKS $$$ 1. mutable 2. mapping 3 eerie oat 4. comma 5. colon (:) 6. keyl) 7. unique 8, del; pop() SOLVED QUESTIONS ——_——— ‘4, What is a dictionary? Ans. A Python dictionary isa mapping of unique keys to values. tis collection of key-value pairs. Dietionaries aregtable which means they eon he Canaed_ 2. How are objects stored in dictionaries different? {HOTS} Ans, The objects or values stored in a dictionary can be of any data type (even the type NONE), but keys can be immutable type-objecs. Eg, strings, integers, et 3. Can sequence operations such as slicing and concatenation be applied to dictionaries? Why? [HOTS] ‘Ans. No, sequence operations such a slicing and concatenation cannot be applied to dictionaries as dictionary is not a sequence. Since it is not maintained in any specific order, operations that depend on a specific order cannot be used, 4. Why can’t lists can be used as keys? [HOTS] Ans. lists cannot be used as keys ina dictionary because they are mutable. And a python dictionary can have ‘only keys of immutable types. 5. If the addition of a new key-value pair causes the size of the dictionary to grow beyond its original size, it shall result in an error. True or false? [HOTS] ‘Ans. Faise. No error shall occur because dictionaries are mutable in nature, they can grow or shrink as and when required. 6. What are the differences between lists and dictionary? Ans. Thedifferencesbetween dietionary and stare that i ists, elementsare enclosed within square bracket whereas in dictionary, key-value pairs are enclosed within curly braces. In dictionary, index value aa of any data type and is called key, but in list, index value is an integer. (Ain eon snrine em nd eee ser crac efoloig a ar-aier(') white icon: a=input ("Enter nane:") beinput ("Enter age:") ; al gg=b isin 1 =d1.key() for i in 1: print (i,'\t',*dl(i)*) i neint (input ("Enter number of entrie while i acinput ("Enter name:") beinput ("Enter age aifal=! isin 1=di.keys 0. for 1 inl: print (i, "\t",d1 (iJ) ' aie aot 8, Write the output of the following codes: (a) >>>dl=(1:10, 2:20, 3:30,4:40,5:50) >>>dl. items () >>pdl keys () >>>dl values () {b) di=(1:10,2:20,3:30, 4:40} d2={5:50, 6:60, 7:70) dl -upaate (d2) print (dl) {c) d=(1:10, 2:20, 3:30, 4:40, 5:50, 6:60, 7:70} del d1[3] print (dl) (a) >>>dl=(1:10, 2:20, 3:30, 4:40, 5:50, 6:60, 7:70) >>>dl_.pop (5) >>>print (dl) (e) >>>a1=(1:10,2:20,3:30,4:40, 5250, 6:60,7:70) Clattog Bown >>>len (dl) >>>dl get (6, 60) >>pdl [61-65 >>odl (8]=35 >>>print (dl) perdi clear () >>>print (dl) Ans. (a) dict_items{ (1, 10), (2, 20), (3, 30), (4, 40), (5, 50)1 dict_keys(1, 2,3, 4, 5] dict_values (10, 20, 30, 40, 50) (b) (1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60, 7: 70) (c) (1: 10, 2: 20, 4: 40, 5: 50, 6: 60, 7: 70) (a) 50 (1: 10, 2: 20, 3: 30, 4: 40, 6: 60, 7: 70) 7 60 (1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 65, 7: 70, B: 35} 0 9. Write a Python program to input ‘n’ names and phone numbers to store it in a dictionary and print the phone number of a particular name. phone=dict () isl neint (input ("Enter number of entries:")) while isso nput ("Enter Name:") nput ("Enter Phone n phone (a}=b isin 1 = phone. keys () x=input ("Enter name to be searched:") for i in 1: if ix print (x,": phone no is:",phone(i]) break else: print(x, "does not exist” ") W Ans. Output: Enter number of entries:4 Enter Name:Riya Enter Phone no: 9999 Enter Name:Aman Enter Phone no:1111 Enter Name: Pulkit Enter Phone no:5555 Enter Name: Ziya Enter Phone no:2222 Enter name to be searched Anan Aman : phone no is: 1111 2 Python program to input names of ‘n’ employees and their salary details like basic salary, house rent and conveyance allowance. Calculate total salary of exch employee and dis nsint (input ("Enter number of entries:")) a while ic-n: 4 mput ("\nEnter name of the employee: *) nt (input ("Enter Basic salary: ")) 7 nt (input ("Enter house rent allowance: ")) casint (input ("Enter conveya: oh) i [Nn] =[basic, hra, ca] isin 1 = dl-keys() print ("\nName", *\t\t', "Net for iini: salary z= dl [il : for j inz: salary=salary+j print (i, '\t\t",salary) Output: Enter number of entries: Enter name of the employe: Enter Basic salary: 100000 Enter house rent allowance: 30000 Enter conveyance allowance: 5000 Note: list is stored in dictionary as value. : Nishant Enter name of the employee: Ram Enter Basic salary: 50000 Enter house rent allowance: 15000 = Enter conveyance allowance: 5000 Enter name of the employee: Aushim Enter Basic salary: 200000 Enter house rent allowance: 50000 Enter conveyance allowance: 10000 Enter name of the employee: Riya ‘ Enter Basic salary: 25000 zi | Enter house rent allowance: 2000 yu Enter conveyance allowance: 1000 | 1 | SW Name, Net Sulury Nishant 135000 Ram 70000 Aushim 260000 Riya 28000 : i itina 111, Write a Python program to input names of 'n’ countries and their capital and currency, store dictionary and display in tabular form, Also search and display for a particular country. Ans, di=dict () ind neint (input ("Enter number of entries while icen: ei ll ee c=input ("Enter Country ‘ | capeanpue enter capitals") { curreinput ("Enter currency of country") ( ai {el=(cap, curr] Holding the country details as a list { Le ie 1 = dl keys () { print ("\nCountry\e\t", "Capital\e\e", "currency") ‘ fore inl: { zed [i] print ("\n',i, "\t\t! ends for j ina: print (j,'\t\t',end="\t\t") # searching ie es x=input ("\nBnter Country to be searched: ") ie . for iin: ' if i-ex: print ("\nCountry\t\t", "Capital\t\t", "Currency\t\t") ‘ zed1 lil i print (*\n', i, "\e\t', enc for j ina: print (j,*\t\t',end="\t\t") Output: Enter number of entries:5 z Fnter Country:Austria Enter Capital: Vienna Enter currency of country:Suro Enter Country:China : Enter Capital:Beijing | Enter currency of country:Yuan : Enter Country: India : i Enter Capital:New Delhi Enter currency of country: Indian Rupee Enter Country: France Enter Capital:Paris Enter currency of country:Euro Enter Country:United Kingdom Enter Capital:London Enter currency of country:Pound arc eu Country capital currency Austria Vienna Euro china Beijing Yuan ? India New Delhi Indian Rupee France Paris Euro United Kingdom London Pound Enter Country to be searched: India country capital Currency India New Delhi Indian Rupee 22. Write a program to find number of occurrences of each letter present in the given string. Ans, word = input ("Enter any word: ") dso for i in word: ALi] = d.get (i,0)41 for k,v in d.items() : print (k, "occured", v, "times") Output: Enter any word: nitinhello n occured 2 times i occured 2 times t occured 1 times h occured 1 times ¢ occured 1 times 1 occured 2 times © occured 1 times 13. ite a program to find number of occurrences of each vowel present in the inputted string. Ans. word = input ("Enter the string : ") vowels = {ta','e!, 1it,'o!, 'u") . a=0 for i in word: if i in-vowels: Ii] = d.get (i,0) +2 for k,v in sorted(d.items()): print (k, "occured", v, "times") Output: Enter the string: Pythonprogramming a occured 1 times i occured 1 times © occured 2 times UNSOLVED QUES TIONS == 11, What is a key-value pair in dictionary? 2. What are the differences between strings and dictionary? 3, Find errors and rewrite the same after correcting the following code: (a) di=(1:10,2.5:20,3:30, 4:40,5:50,6:60,7/70}, (b) di.(9)=90 (e) del di (2) : (4) pop dita] {e) di.iteni() ry (A dlkey() (g) di.value() ; (h) dl.gets (4,80) () di.tenQ 1 @ di.clears() 4. Suppose peedi=(1:'one',2:'two', 3 ‘three’, 4:'four') >>>d2=(5:"£ive',6:" six') Write the output of the following code: (a) >>>al. items () >>>dl keys () >>>dl.values () >>>d1 update (d2) >>>len (dl) (b) >>>de1 an {3} >>>print (dl) >>>dl pop (4) >>>print (dl) >>>dl [8] ="eight! >>>print (dl) >e>dl.clear() >>>print (dl) 5. Write a Python program to input display the same. Also accept a of that class, ‘n’ classes and names of their class teachers to store it in a dictionary and Particular class from the user and display the name of the class teacher 6. Write a program to store student names and their Percentage ina dictionary, delete a particular student ‘name from the dictionary Y. Also display dictionary after deletion. 7. Write a Python program to input names of ‘n’ cust fomers and their details like items bought, cost and phone number, store it in a dictionary and display all the details in a tabular form, 8. What type of objects can be used as keys in dictionaries? 9. Can you check from a value inside 3 dictiona FY Using in operator? How will you check for a value i dictionary using in operator? 10, How is clear() function different from del() statement? 11, The following code is giving some error. Find out the error and write the corrected code aL = ("ams1, Lia", (1, "a") two") 12. What will be the output of the following code: dl = (5: [6,7,8], "a": (1,2,3)} print (dl. keys ()) print (dl.values ()) 13, Predict the output: (a) d= ((1,2)21, (2,3) 22) print (d[1,2]) () d= {tated tb print (d{'a’, 'b')) 14, Write a program that asks the user to enter product names and whose keys are the product names and whose values are the products and prices, allow them to repeatedly enter a or message if the product is notin the dictionary, Prices. Store all of these in a dictionary prices. When the user is done entering product name and print the corresponding price 15. Write a Python program that accepts a value and checks whether the in dictionary or not. Ifit is present, check for the frequen key otherwise print an error message. putted value is part of given cy ofits occurrence and print the corresponding ne 0 OBB 28 8282842 AAS

Вам также может понравиться