Chap 10 - Worksheet
Chap 10 - Worksheet
2) Suppose T is a tuple . Which of the following statement(s) would give an error after
executing the following code ?
T = (100,101,-89,45)
print(sum(T)) #Statement 1
print(T.sort()) #Statement 2
x, y, z, w = T #Statement 3
print(sorted(T)) #Statement 4
(a) Statement 1 (b) Statement 2 (c) Statement 3 (d) Statement 4
3) Which of the following will delete the key_value pair for key = "tiger" in dictionary ?
di = {"loin" :"wild", "tiger" : "wild", "cat" : "domestic" , "dog" : "domestic"}
(a) del di["tiger"] b) delete(d1["tiger" : "wild"])
(c) delete(di.["tiger"]) (d) del(di.["tiger])
8) Find the error. Following code intends to create a tuple with three identical strings. But
even after successfully executing following code(No error reported by python), The len()
returns a value different from 3. Why?
tup1=(‘Mega’)*3
print(len(tup1))
9) WAP that creates a tuple storing first 9 terms of Fibonacci series.
10) When would sum() not work for tuples?
11) Do min() & max() always work for tuples?
12) Is the working of in operator and tuple.index() same?
13) What is the output of the following?
S=0
d1={“cat”:12,’dog’: 6, ‘elephant’:23,’bear’:20}
for a in d1:
if len(a)>3:
s=s+d1[a]
print(s)
14) WAP to check if a dictionary is empty.
15) WAP to find the highest 2 values in a dictionary.
16) WAP to create a dictionary from a string
17) Discuss the working of copy() if (i) the values are of immutable types, (ii) the values are
of mutable types.
18) How is pop() different from popitem()?
19) Can you use sum() for calculating the sum of values of a dictionary?
20) Why is a dictionary termed as an unordered collection of objects?
21) Write a Program in Python to create a dictionary with names of employees, their salary
and access them.
Output format:
Enter employee name : Amit
Enter employee salary : 20000
Do you want to add more employee details(y/n) :y
Enter employee name : Sachin
Enter employee salary : 25000
Do you want to add more employee details(y/n) :y
Enter employee name : Saanvi
Enter employee salary : 28000
Do you want to add more employee details(y/n) :n
{'Amit': 20000, 'Sachin': 25000, 'Saanvi': 28000}
22. Write a program in python, to input a dictionary Emp which contains eid and ename
as key value pair and displays the name in uppercase of the values whose names are longer
than 5
characters.
For example:
Emp = {101: 'Rahul', 102: 'Hardik', 103: 'Virat', 104:'Jaspreet'}
The output should be:
HARDIK
JASPREET
25. Write a Python program to calculate the product, multiplying all the numbers in a given
tuple.
Original Tuple:
(4, 3, 2, 2, -1, 18)
Product - multiplying all the numbers of the said tuple: -864
Original Tuple:
(2, 4, 8, 8, 3, 2, 9)
Product - multiplying all the numbers of the said tuple: 27648
26.
1.Write a program in python, to input a dictionary Emp
which contains eid and ename as key value pair and
displays the name in uppercase of the values whose
names are longer than 5 characters.
For example: Emp = {101: 'Rahul', 102: 'Hardik', 103:
'Virat', 104:'Jaspreet'}
The output should be:
HARDIK
JASPREET
2. Write a Python program to reverse a tuple.
3. Write a Python program to convert a given string into
list ,and the list into a tuple.
4. Write a Python program to calculate the product,
multiplying all the numbers in a given tuple.
Original Tuple:
(4, 3, 2, 2, -1, 18)
Product - multiplying all the numbers of the said tuple:
-864
Original Tuple:
(2, 4, 8, 8, 3, 2, 9)
Product - multiplying all the even numbers of the said
tuple: 1024