Python Dictionary and Tuple

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Summer Fields School

DLF, Phase-I, Gurugram, Haryana

Subject: Computer Science(083) Topic: (Ch: Tuple and Dictionary)

Name ……………………..… Class & Sec: XI-……. Date ………/2022

1. Answer the following questions:

a. Differentiate between List, String, Tuple and Dictionary.


b. Write statement to declare empty tuple and dictionary.
c. How keys and values methods are different? Explain with example.
d. What are the conditions to be followed while defining key for a dictionary?
e. Why lists cannot be used as keys in dictionary?
f. How will you convert a string into a tuple?
g. How will you create a dictionary using tuple?
2. Consider the following dictionary and write output for each statement:

company={1:"IBM",2:"Microsoft",3:"Apple",4:"Intel"}
del company[4]
i=0
i=i+1
print("Result :",i," : ",company)
company[4]="Intel"
company[5]="Dell"
l=len(company)
i=i+1 print("Result :",i," : ",l)
comp1=company.copy()
i=i+1 print("Result :",i," : ",company)
i=i+1 print("Result :",i," : ","1:",company.get(1))
i=i+1 print("Result :",i," : ",company.setdefault(3,"telnet"))
company.update(comp1)
i=i+1 print("Result :",i," : ",company)
keys = {'a', 'e', 'i', 'o', 'u' }
value = 'vowel'
vowels = dict.fromkeys(keys, value)
i=i+1 print("Result :",i," : ",vowels)
i=i+1 print("Result :",i," : ",company.keys())
i=i+1 print("Result :",i," : ",company.items())
i=i+1 element = company.pop(3)
print("Result :",i," : ",'The popped element is:', element)
result = company.popitem()
i=i+1 print("Result :",i," : ",'person = ',company)
i=i+1 print("Result :",i," : ",'Return Value = ',result)
i=i+1 print("Result :",i," : ",company.values())
i=i+1 print("Result :",i," : ",all(company))
i=i+1 print("Result :",i," : ",any(company))
i=i+1 print("Result :",i," : ",sorted(company))
people={1:{"name":"Shivam","age":19},2:{"name":"ashish","age":50}}
i=i+1 print("Result :",i," : ",people)
i=i+1 print("Result :",i," : ",people[1]["name"])
i=i+1 print("Result :",i," : ",people[2]["name"])
people[3]={"name":"priya","age":45}
i=i+1 print("Result :",i," : ",people)
del people[2]
i=i+1 print("Result :",i," : ",people)

3. Multiple Choice Questions:

1. Which of the following statements create a dictionary?


a) d = {} b) d = {“john”:40, “peter”:45}
c) d = {40:”john”, 45:”peter”} d) All of the mentioned
2. Read the code shown below carefully and pick out the keys? d = {"john":40,
"peter":45}
a) “john”, 40, 45, and “peter” b) “john” and “peter”
c) 40 and 45 d) d = (40:”john”, 45:”peter”)
3. What will be the output? d = {"john":40, "peter":45} "john" in d
a) True b) False c) None d) Error
4. What will be the output?
d1 = {"john":40, "peter":45} d2 = {"john":466, "peter":45} d1 == d2
a) True b) False c) None d) Error
5. What will be the output? d1 = {"john":40, "peter":45} d2 = {"john":466, "peter":45}
d1 > d2 a) True b) False c) Error d) None
6. What is the output? d = {"john":40, "peter":45} d["john"]
a) 40 b) 45 c) “john” d) “peter”
7. Suppose d = {“john”:40, “peter”:45}, to delete the entry for “john” what command do
we use a) d.delete(“john”:40) b) d.delete(“john”) c) del d[“john”]. d) del
d(“john”:40)
8. Suppose d = {“john”:40, “peter”:45}. To obtain the number of entries in dictionary
which command do we use?
a) d.size() b) len(d) c) size(d) d) d.len()
9. What will be the output? d = {"john":40, "peter":45} print(list(d.keys()))
a) [“john”, “peter”]. b) [“john”:40, “peter”:45].
c) (“john”, “peter”) d) (“john”:40, “peter”:45)
10. Suppose d = {“john”:40, “peter”:45}, what happens when we try to retrieve a value
using the expression d[“susan”]?
a) Since “susan” is not a value in the set, Python raises a KeyError exception
b) It is executed fine and no exception is raised, and it returns None
c) Since “susan” is not a key in the set, Python raises a KeyError exception
d) Since “susan” is not a key in the set, Python raises a syntax error
11. Which of these about a dictionary is false?
a) The values of a dictionary can be accessed using keys
b) The keys of a dictionary can be accessed using values
c) Dictionaries aren’t ordered
d) Dictionaries are mutable
12. Which of the following is not a declaration of the dictionary?
a) {1: ‘A’, 2: ‘B’} b) dict([[1,”A”],[2,”B”]]) c) {1,”A”,2”B”} d) { }
13. What is the output of the following code?
a={1:"A",2:"B",3:"C"} for i,j in a.items(): print(i,j,end=" ")
a) 1 A 2 B 3 C b) 1 2 3 c) A B C d) 1:”A” 2:”B” 3:”C”
14. What is the output of the following piece of code? a={1:"A",2:"B",3:"C"}
print(a.get(1,4))
a) 1 b) A c) 4 d) Invalid syntax for get
method
15. What is the output of the following code? a={1:"A",2:"B",3:"C"} print(a.get(5,4))
a) Error, invalid syntax b) A c) 5 d) 4
16. What is the output of the following code? a={1:"A",2:"B",3:"C"}
print(a.setdefault(3))
a) {1: ‘A’, 2: ‘B’, 3: ‘C’} b) C
c) {1: 3, 2: 3, 3: 3} d) No method called setdefault() exists for
dictionary
17. What is the output of the following code? a={1:"A",2:"B",3:"C"}
a.setdefault(4,"D") print(a)
a) {1: ‘A’, 2: ‘B’, 3: ‘C’, 4: ‘D’}. b) None. c) Error. d) [1,3,6,10].
18. What is the output of the following code?
a={1:"A",2:"B",3:"C"} b={4:"D",5:"E"} a.update(b) print(a)
a) {1: ‘A’, 2: ‘B’, 3: ‘C’} b) Method update() doesn’t exist for dictionaries
c) {1: ‘A’, 2: ‘B’, 3: ‘C’, 4: ‘D’, 5: ‘E’} d) {4: ‘D’, 5: ‘E’}
19. What is the output of the following code?
a={1:"A",2:"B",3:"C"} b=a.copy() b[2]="D" print(a)
a) Error, copy() method doesn’t exist for dictionaries b) {1: ‘A’, 2: ‘B’, 3: ‘C’}
c) {1: ‘A’, 2: ‘D’, 3: ‘C’} d) “None” is printed
20. What is the output of the following code?
a={1:"A",2:"B",3:"C"} a.clear() print(a)
a) None b) { None:None, None:None, None:None}
c) {1:None, 2:None, 3:None} d) { }
21. Which of the following is a Tuple?
a. [1,2,3,4] b. {1,2,3,4} c. (1,2,3,4) d. None
22. Consider two tuples t1 and t2 as shown below: T1=(1,2,4,3) T2=(1,2,3,4)
What will be: print(T1<T2)
a. True b. False c. Error d. None
23. Suppose t = (1, 2, 4, 3), which of the following is incorrect?
a. print(t[3]) b. t[3]=45 c. print(max(t)) d. print(len(t))
24. What will be the output of the following Python code? >>>t=(1,2,4,3) >>>t[1:3]
a. (1,2) b. (1,2,4) c. (2,4) d. (2,4,3)

25. What will be the output of the following Python code?


>>>t = (1, 2, 4, 3, 8, 9)
>>>[t[i] for i in range(0, len(t), 2)]
a. [2, 3, 9] b. [1,2,,3,8,9][1,4,8] c. (1,4,8) d. None
26. What will be the output of the following Python code?
>>>t = (1, 2) >>>2 * t
a. [1,2,1,2) b. (2,1,2,1) c. (1,1,2,2) d. None
27. What will be the output of the following Python code?
>>>t1 = (1, 2, 4, 3) >>>t2 = (1, 2, 3, 4) >>>t1 < t2
a. True b. False c. None d. Error
28. What will be the output of the following Python code?
>>>my_tuple = (1, 2, 3, 4) >>>my_tuple.append( (5, 6, 7) ) >>>print len(my_tuple)
a. 1 b. 2 c. 5 d. Error
29. What is the correct code to find the length of a Tuple?
a. length(t1) b. t1.length() c. len(t1) d. t1.len()
30. Method to find the minimum of a tuple is:
a. min(t1) b. min.minimum c. t1.minimum() d. t1.min()

4. Write Python programs for the following:

a. WAP to input ‘N’ numbers of student’s name in a tuple and print the tuple in ascending
order of name?
b. Write a program that inputs two tuples seq_a and seq_b and prints True if every
element in seq_a is also an element of seq_b, else prints False.
c. Write a program that creates a tuple storing first 9 terms of Fibonacci Series.
d. Given a tuple pairs=((2,5),(4,2),(9,8),(12,10)), count the number of pairs(a,b) such that
both a and b are even.
e. Write a program as per the specification: Return the length of the shortest string in the
tuple of strings str_tuple. Precondition: the tuple will contain at least one element.
f. Write a Program to create a dictionary named year whose keys are month
names and values are their corresponding number of days.Write a Program that
repeatedly asks the user to enter product names and prices. Store all of them in a
dictionary whose keys are product names and values are prices. And also write a code
to search an item from the dictionary.
g. WAP to take the number of students as input then ask marks for five subjects. If the
total marks of any student are less than 200 the print ‘Fail’ or else print ‘Pass’. Use a
dictionary to store the student’s name as key and marks as values?
h. Write a Program that list the overlapping keys of the two dictionaries. list if the key of
D1 is the same as that of D2.
i. WAP to create a dictionary containing customer name with respective phone numbers,
search the name of the customer and print the phone number of that name.
j. There are 20 employees in a company write program to enter the PAN number, name
and annual salary and calculate the tax of all employees. The following table shows the
income tax slabs?
Annual income slab Tax rate
Income up to 2,50,000 No tax
Income from 2,50,000 to 5,00,000 5%
Income from 5,00,000 to 10,00,000 20%
Income more than 10,00,000 30%
Surcharge: 10% of income tax where total income exceed 50, 00,000 to
1, 00, 00,000 and 15% of income tax where total income exceeds 1, 00, 00,000.
Cess: 4% of total income tax

You might also like