WORKSHEET - REVISION OF PYTHON
1. Which of the following is a Python tuple?
a) [1, 2, 3]
b) (1, 2, 3)
c) {1, 2, 3}
d) {}
2. 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))
3. What is the output of the following code:
t=(1,2,4,3)
print( t[1:3] )
a) (1, 2)
b) (1, 2, 4)
c) (2, 4)
d) (2, 4, 3)
. 4. What will be the output of the following Python code?
t=(1,2,4,3)
print( t[1:-1])
a) (1, 2)
b) (1, 2, 4)
c) (2, 4)
d) (2, 4, 3)
5. What will be the output of the following Python code?
d={“John”:40,”Peter”:45}
print(d[“John”])
a.40
b. 45
c. “john”
d. “peter”
6. What will be the output of the following Python code?
t= (1,2)
print(2*t)
Page 23
a) (1, 2, 1, 2)
b) [1, 2, 1, 2]
c) (1, 1, 2, 2)
d) [1, 1, 2, 2]
7. What will be the output of the following Python code?
T1 =(1,2,3,4)
T2=(1,2,4,3)
print( T1 < T2 )
a) True
b) False
c) Error
d) None
8. What will be the output of the following Python code?
T=(3, 4)
[Link](5)
print(T)
a) 1
b) 2
c) 5
d) Error
9. Which of the following creates a tuple?
a. tuple1=("a","b")
b. tuple1[2]=("a","b")
c. tuple1=(5)*2
d. None of the above
10. Choose the correct option with respect to Python.
a. Both tuples and lists are immutable.
b. Tuples are immutable while lists are mutable.
c. Both tuples and lists are mutable.
d. Tuples are mutable while lists are immutable.
[Link] the correct option.
a. In Python, a tuple can contain only integers as its elements.
b. In Python, a tuple can contain only strings as its elements.
Page 24
c. In Python, a tuple can contain both integers and strings as its elements.
d. In Python, a tuple can contain either string or integer but not both at a time.
12. What will be the output of below Python code?
tuple1=(5,1,7,6,2)
[Link](2)
print(tuple1)
a. (5,1,6,2)
b. (5,1,7,6)
c. (5,1,7,6,2)
d. Error
13. What will be the output of below Python code?
tupl=("annie","hena","sid")
print(tupl[-3:0])
a. ("annie")
b. ()
c. None
d. Error as slicing is not possible in tuple.
14. Which of the following options will not result in an error when performed on tuples
in Python where tupl=(5,2,7,0,3)?
a. tupl[1]=2
b. [Link](2)
c. tupl1=tupl+tupl
d. [Link]()
15. Which one of the following is correct?
a. Dictionary is the only mutable data type
b. Dictionary is an immutable data type
c. Both list and dictionary are mutable data type
d. All of the above
16. What will be the output of above Python code?
d1={"abc":5,"def":6,"ghi":7}
print(d1[0])
a. Abc
Page 25
b. 5
c. {"abc":5}
d. Error
17. What will be the result of above Python code?
dict={"Joy":1,"Rachel":2}
[Link]({"Philip":2})
print(dict)
a. {"Joy":1,"Rachel":2,"Philip":2}
b. {"Joy":1,"Rachel":2}
c. {"Joy":1,"Philip":2}
d. Error
18. Which of the following functions will return the key, value pairs of a dictionary?
a. evs()
[Link]()
c. items()
d. all of the above
19. Let d = {“Amit”:40, “Abhay”:45}
To delete the entry for “Amit” what command do we use?
a. [Link](“Amit”:40)
b. del d(“Amit”:40)
c. del d[“Amit”]
d. [Link](“Amit”)
20. What will be the output of the following Python code snippet?
d1 = {“Amit”:34, “Arnav”:45}
d2 = {“Amit”:46, “Arnav”:45}
d1 == d2
a. True
b. False
c. None
d. Error
21. What will be the output after the following statements?
x = {1:’Jan’, 2:’Feb’, 3:’March’, 4:’April’}
print(x[2])
Page 26
a. Jan
b. Feb
c. March
d. April
22. What would the following code print?
d = {‘spring’: ‘autumn’, “autumn”: “fall”, “fall”:”spring”}
print (d[“autumn”])
a. autumn
b. fall
c. spring
d. Error
[Link] will be the result of the following code?
d1 = {“abc”:5,”def”:6, “ghi”:7}
print (d1[0])
a. Abc
b. 5
c. (“abc” : 5)
d. Error
24. Which of the following will modify key_value pair for key = “lion” in dictionary?
di = {“lion” : “wild”, “tiger” : “wild”, “cat”:”domestic” , “dog” : “domestic”}
a. [Link]({“lion”:”king”})
b. di[“lion”]=”king”
c. None
d. Both a and b
25. What is printed by the following statements?
D1 = {“cat”:17, “dog”:6, “elephant”:23, “bear”:20}
print (25 in D1)
a. True
b. False
c. Error
d. None
26. What is printed by the following statements?
Page 27
D1 = {“cat”:17, “dog”:6, “elephant”:23, “bear”:20}
print (“dog” in D1)
a. True
b. False
c. Error
d. None
27. Which of the following data types fall under mappings:
a. List
b. Tuple
c. String
d. Dictionary
28. Which of the following will give error?
Let dict1={“a”:1, “b” :2, “c” :3 }
a. print(len(dict1))
b. print([Link]("b"))
c. dict1["a"]=5
d. None of these.
29. Which one of the following is correct?
a. In python, a dictionary can have two same keys with different values.
b. In python, a dictionary can have two same values with different keys
c. In python, a dictionary can have two same keys or same values but cannot have
two same key-value pair
d. In python, a dictionary can neither have two same keys nor two same values.
[Link] of the following will delete key_value pair for key="tiger" in dictionary?
dic={"lion" : "wild","tiger" : "wild","cat" : "domestic","dog" : "domestic"}
a. del dic["tiger"]
b. dic["tiger"].delete()
c. delete(dic.["tiger"])
d. del(dic.["tiger"])
Page 28
MCQ Answers:
QNO OPTION QNO OPTION QNO OPTION
1 b 11 c 21 b
2 b 12 d 22 b
3 c 13 b 23 d
4 c 14 c 24 d
5 a 15 c 25 b
6 a 16 d 26 a
7 a 17 a 27 d
8 d 18 c 28 d
9 a 19 c 29 b
10 b 20 a 30 a
OUTPUT FINDING QUESTIONS
1. Find output of the following code:
mydict={'a':27,'b':43,'c':25,'d':30}
val=''
for i in mydict:
if i>val:
val=i
valb=mydict[i]
print(val)
print(valb)
print (30 in mydict)
2. Find output:
d1={5:"number","a":"string",(1,2):"tuple"}
for x in [Link]():
print(x,":",d1[x],end="")
print(d1[x]*3)
print()
Page 29
3. Find output:
d=dict()
d['left']='<'
d['right']='>'
print(d['left'] and d['right']or d['right'] and d['left'])
4. Find output:
text="abracadabraaabbccrr"
counts={}
lst=[]
for word in text:
if word not in lst:
[Link](word)
counts[word]=0
counts[word]=counts[word]+1
print(counts)
5. Find output:
d={1:"a",2:"b",3:"c"}
d1={}
for x in d:
d1[d[x]]=x
print(d1)
6. Find output:
d1={1:"a",2:"b"}
d2={3:"c",2:"d"}
for x in d1:
if x in d2:
print(x)
7. Find output:
Page 30
tuple = {}
tuple[(1,2,4)] = 8
tuple[(4,2,1)] = 10
tuple[(1,2)] = 12
_sum = 0
for k in tuple:
_sum += tuple[k]
print(len(tuple) + _sum)
8. Find output:
T = 'abcde'
a, b, c, d, e = T
b = c = '*'
T = (a, b, c, d, e)
print(T)
9. Find output:
T = (1, 2, 3, 4, 5, 6, 7, 8)
print(T[[Link](5)], end = " ")
print(T[T[T[6]-3]-6])
[Link] output:
D = dict()
for i in range (3):
for j in range(2):
D[i] = j
print(D)
11. Find output
d1={"a":45,"b":78}
d2={"c":45,"b":88}
[Link](d2)
print(d1)
Page 31
[Link] output:
d1={"a":45,"b":78}
d2={"c":45,"b":88}
d3=[Link]()
print([Link]())
print([Link]())
print([Link]())
[Link] output:
d1={101:["A","B"],102:["C","D"],103:["E","F"]}
for i in d1:
for j in d1[i]:
print(j, end=" ")
print()
[Link] output:
d1={1:"Mars",2:"Venus",3:"Earth"}
d1[3]="Jupiter"
print(d1)
print([Link]())
[Link] output:
d1={1:"Mars",2:"Venus",3:"Earth"}
for i in d1:
s=d1[i]
print(len(s))
Answers- Output finding
1. Ans:
d
30
False
2. Ans:
5 : numbernumbernumbernumber
a : stringstringstringstring
(1, 2) : tupletupletupletuple
3. Ans:
Page 32
>
4. Ans:
{'a': 7, 'b': 4, 'r': 4, 'c': 3, 'd': 1}
5. Ans:
{'a': 1, 'b': 2, 'c': 3}
6. Ans:
2
7. Ans
33
8. Ans:
('a', '*', '*', 'd', 'e')
9. Ans:
5 8
10. Ans:
{0: 1, 1: 1, 2: 1}
11. Ans:
{'a': 45, 'b': 88, 'c': 45}
12. Ans:
dict_keys(['a', 'b'])
dict_values([45, 88])
dict_keys(['c', 'b'])
13. Ans:
AB
CD
EF
[Link]:
{1: 'Mars', 2: 'Venus', 3: 'Jupiter'}
dict_items([(1, 'Mars'), (2, 'Venus'), (3, 'Jupiter')])
[Link]:
4
5
5
Page 33
PROGRAMS
1. Write a program to read a tuple from the user using loop
n=int(input("Enter the number of elements:"))
t=tuple()
for i in range(n):
a=int(input("Enter number"))
t=t+(a,)
print(t)
2. Read a tuple from user and find the mean of the elements
t=eval(input("Enter a tuple of numbers"))
s=sum(t)
mean=s/len(t)
print("Mean is ",mean)
3. Read a tuple from the user and print its reverse .
t=eval(input("Enter a tuple of numbers"))
print("Revrse is : ",t[::-1])
4. Read a tuple from the user and add an element at the end of it using list.
t=eval(input("Enter a tuple of numbers"))
l=list(t)
a=int(input("Enter the number to add at the end:"))
[Link](a)
t=tuple(l)
print(t)
5. Read a tuple from the user and modify it so that all the even numbers get
multiplied by 2 and all odd numbers get multiplied by 3.
t=eval(input("Enter a tuple of numbers"))
l=list(t)
for i in range(len(l)):
if l[i]%2==0:
l[i]=l[i]*2
else:
l[i]=l[i]*3
t=tuple(l)
print(t)
Page 34
6. Write a program to interchange to tuples. For example, if t1=(1,2) and t2=(3,4),
then the tuples must be changed as t1=(3,4) and t2=(1,2)
t1=eval(input("Enter first tuple"))
t2=eval(input("Enter second tuple"))
t1, t2=t2, t1
print(t1, t2)
7. Write a program to read a tuple from the user and create a list from the tuple
which contains unique elements of the tuple.
t1=eval(input("Enter first tuple"))
l=[]
for i in t1:
if [Link](i)==1:
[Link](i)
print(l)
8. Create a dictionary of books with book number as key and book name as value
n=int(input("Enter number of books:"))
d={}
for i in range(n):
key=int(input("Enter book number:"))
val=input("Enter name:")
d[key]=val
print(d)
9. Create a dictionary of books with book number as key and book name as value.
Read a book number and search for it.
n=int(input("Enter number of books:"))
d={}
for i in range(n):
key=int(input("Enter book number:"))
val=input("Enter name:")
d[key]=val
print(d)
x= int(input("Enter book number to search:"))
if x in d:
print("Found")
else:
print("Not Found")
Page 35
[Link] a dictionary of students with roll number, name and mark with roll
number as key and name as values in the form of tuple.
n=int(input("Enter number of students:"))
d={}
for i in range(n):
key=int(input("Enter roll no:"))
val1=input("Enter name:")
val2=int(input("Enter total mark:"))
d[key]=(val1,val2)
print(d)
[Link] a dictionary of students with roll number, name and mark with roll
number as key and name as values in the form of tuple. Display the details of
student who has scored marks above 75
n=int(input("Enter number of students:"))
d={}
for i in range(n):
key=int(input("Enter roll no:"))
val1=input("Enter name:")
val2=int(input("Enter total mark:"))
d[key]=(val1,val2)
print(d)
for i in d:
t=d[i]
if t[1]>75:
print(d[i])
[Link] a dictionary of items with item number and price. Display the sum of all
items.
n=int(input("Enter number of items:"))
d={}
for i in range(n):
key=int(input("Enter item no:"))
price=int(input("Enter price"))
d[key]=price
print(d)
s=0
for item in d:
s+=d[item]
print("Sum is ",s)
Page 36