0% found this document useful (0 votes)
190 views15 pages

Python Dictionary Output Worksheet

This document contains 48 questions related to dictionaries in Python. The questions cover dictionary basics like creating, accessing and updating dictionaries as well as more advanced topics like merging dictionaries, removing duplicates, checking for empty dictionaries and generating dictionaries. The questions are in a worksheet format and include sample code with expected outputs to test dictionary skills.

Uploaded by

Sanchi Khurana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
190 views15 pages

Python Dictionary Output Worksheet

This document contains 48 questions related to dictionaries in Python. The questions cover dictionary basics like creating, accessing and updating dictionaries as well as more advanced topics like merging dictionaries, removing duplicates, checking for empty dictionaries and generating dictionaries. The questions are in a worksheet format and include sample code with expected outputs to test dictionary skills.

Uploaded by

Sanchi Khurana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Visit Python4csip.

com for more updates

WORKSHEET – DICTIONARY

1 What will be the output of following code-


a={1:"A",2:"B",3:"C"}
for i in a:
print(i,end=" ")

2 What will be the output of following code-


a={i: 'Hi!' + str(i) for i in range(5)}
a

3 What will be the output of following code-


D = dict()
for i in range (3):
for j in range(2):
D[i] = j
print(D)

4 What will be the output of following code-


a={i: i*i for i in range(6)}
a
Ans:

{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

1|Page
Visit Python4csip.com for more updates

5 What will be the output of following code-


a={}
a[2]=1
a[1]=[2,3,4]
print(a[1][1])

6 What will be the output of following program:


dictionary = {1:'1', 2:'2', 3:'3'}
del dictionary[1]
dictionary[1] = '10'
del dictionary[2]
print(len(dictionary))

7 Predict the Output:

dict1 = {"name": "Mike", "salary": 8000}


temp = dict1.pop("age")
print(temp)

8 What will be the output of following program:


dict1 = {"key1":1, "key2":2}
dict2 = {"key2":2, "key1":1}
print(dict1 == dict2)

9 What will be the output of following program:


dict={"Virat":1,"Rohit":2}
dict.update({"Rahul":2})

2|Page
Visit Python4csip.com for more updates

print(dict)

10 What will be the output of following program:


a=dict()
a[1]

11 What will be the output of following program:


a={}
a.fromkeys([1,2,3],"check")

12 What will be the output of following program:


a={}
a['a']=1
a['b']=[2,3,4]
print(a)

13 What will be the output of following program:


a = {}
a[1] = 1
a['1'] = 2
a[1.0]=4
count = 0
for i in a:

3|Page
Visit Python4csip.com for more updates

count += a[i]
print(count)

14 What will be the output of following program:


test = {1:'A', 2:'B', 3:'C'}
del test[1]
test[1] = 'D'
del test[2]
print(len(test)

15 What will be the output of following program:


test = {1:'A', 2:'B', 3:'C'}
test = {}
print(len(test))

16 What will be the output of following program:


a={1:"A",2:"B",3:"C"}
del a
print(a)

17 What will be the output of following program:


a={1:"A",2:"B",3:"C"}
for i in a:
print(i,end=" ")

4|Page
Visit Python4csip.com for more updates

18 What will be the output of following program:


a={1:5,2:3,3:4}
a.pop(3)
print(a)

19 What will be the output of following program:


a={1:5,2:3,3:4}
print(a.pop(4,9))
print()

20 What will be the output of following program:


a={1:"A",2:"B",3:"C"}
a.setdefault(4,"D")
print(a)

21 What will be the output of following program:


a={1:"A",2:"B",3:"C"}
print(a.setdefault(3))

5|Page
Visit Python4csip.com for more updates

22 What will be the output of following program:


a={1:"A",2:"B",3:"C"}
b=a.copy()
b[2]="D"
print(a)

23 What will be the output of the program?


a = {}
a[1] = 1
a['1'] = 2
a[1]=a[1]+1
count = 0
for i in a:
count += a[i]
print(count)

24 What will be the output of following program:


box = {}
jars = {}
crates = {}
box['biscuit'] = 1
box['cake'] = 3
jars['jam'] = 4
crates['box'] = box
crates['jars'] = jars
print(len([crates]))

6|Page
Visit Python4csip.com for more updates

25 What will be the output of following program:


dict = {'c': 97, 'a': 96, 'b': 98}
for _ in sorted(dict):
print (dict[_])

26 What will be the output of following program:


rec = {"Name" : "Python", "Age":"20"}
r = rec.copy()
print(id(r) == id(rec))

27 What will be the output of following program:


rec = {"Name" : "Python", "Age":"20", "Addr" : "NJ", "Country" : "USA"}
id1 = id(rec)
del rec
rec = {"Name" : "Python", "Age":"20", "Addr" : "NJ", "Country" : "USA"}
id2 = id(rec)
print(id1 == id2)

28 What will be the output of following program:


my_dict = {}
my_dict[(1,2,4)] = 8
my_dict[(4,2,1)] = 10

7|Page
Visit Python4csip.com for more updates

my_dict[(1,2)] = 12
sum = 0
for k in my_dict:
sum += my_dict[k]
print (sum)
print(my_dict)

29 What will be the output of following program:


my_dict = {}
my_dict[1] = 1
my_dict['1'] = 2
my_dict[1.0] = 4
sum = 0
for k in my_dict:
sum += my_dict[k]
print (sum)

30 What will be the output of following program:


arr = {}
arr[1] = 1
arr['1'] = 2
arr[1] += 1
sum = 0
for k in arr:
sum += arr[k]
print (sum)

8|Page
Visit Python4csip.com for more updates

31 What will be the output of following program:


a = {'a':1,'b':2,'c':3}
print (a['a','b'])

32 What will be the output of following program:


a = {(1,2):1,(2,3):2}
print(a[1,2])

33 What will be the output of following program:


a={ 1:'a', 2:'b', 3:'c'}
for i,j in a.items():
print(i,j,end="#")

34 What will be the output of following program:


aList = [1,2,3,5,6,1,5,6,1]
fDict = {}
for i in aList:
fDict[i] = aList.count(i)
print (fDict)

35 What will be the output of following program:

9|Page
Visit Python4csip.com for more updates

plant={}
plant[1]='Rohit'
plant[2]='Sharma'
plant['name']='Ishant'
plant[4]='Sharma'
print (plant[2])
print (plant['name'])
print (plant[1])
print (plant)

36 What will be the output of following program:


dict = {'Name': 'Rohit', 'Age': 30}
print (dict.items())

37 Write a Python program to iterate over dictionaries using for loops


Ans:
d = {"blue" : 1, "green" : 2, "yellow" : 3}
for key,value in d.items():
print(key, value)

38 Write a Python script to merge two Python dictionaries

Ans:

d1 = {"a": "100", "b": "200"}

10 | P a g e
Visit Python4csip.com for more updates

d2 = {"x": "4", "y": "500"}


d = d1.copy()
d.update(d2)
print(d)
print(d1.keys())
print(d1)
print(d1.values())

39 Write a Python program to get the maximum and minimum value in a


dictionary.

Ans:
d1 = {"a": 500, "b": 200,"c":50,"d":150}
print(d1)
d = sorted(d1)
min=d[0]
max=d[-1]
print(min, "has min val")
print(max,"Has max val")
40 Write a Python program to multiply all the items in a dictionary
Ans:

d = {'n1': 5,'n2': 2,'n3': 3}


res = 1
for key in d:
res= res*d[key]
print(res)

41 Write a Python program to remove duplicates from Dictionary

Ans:

dict = {'one': 1, 'two': 2, 'three': 3, 'four': 1}


#print(dict)

11 | P a g e
Visit Python4csip.com for more updates

#print(dict.items())
res = {}
for key,value in dict.items():
if value not in res.values():
res[key] = value
print(res)

42 Write a Python program to check a dictionary is empty or not.

Ans:
my_dict = {}
if not bool(my_dict):
print("Dictionary is empty")
else:
print("Dictionary is not empty")

43 Write a Python program to sum all the items in a dictionary.

Ans:

dict = {'n1': 1, 'n2': 2, 'n3': 3}


print(sum(dict.values()))

44 Write a Python script to concatenate following dictionaries to create a new one


.
dic1 = {1:10,2:20}
dic2 = {3:30,4:40}
dic3 = {5:50,6:60}
Ans:

dic = {}
dic1 = {1:10,2:20}
dic2 = {3:30,4:40}
dic3 = {5:50,6:60}

12 | P a g e
Visit Python4csip.com for more updates

for d in (dic1, dic2, dic3):


dic.update(d)
print(dic)
45 Python Program to Add a Key-Value Pair to the Dictionary

Ans:

key=int(input("Enter the key (int) to be added:"))


value=int(input("Enter the value for the key to be added:"))
d={}
d.update({key:value})
print("Updated dictionary is:")
print(d)

46 Python Program to Check if a Given Key Exists in a Dictionary or Not

Ans:
d={'A':1,'B':2,'C':3}
key=input("Enter key to check:")
if key in d.keys():
print("Key is present and value of the key is:")
print(d[key])
else:
print("Key isn't present!")
47 Python Program to Generate a Dictionary that Contains Numbers (between 1
and n) in the Form (x,x*x).

Ans:
n=int(input("Enter a number:"))
d={x:x*x for x in range(1,n+1)}
print(d)

13 | P a g e
Visit Python4csip.com for more updates

48 Python Program to Create a Dictionary with Key as First Character and Value
as Words Starting with that Character.

Ans:

test_string=input("Enter string:")
l=test_string.split()
d={}
for word in l:
if(word[0] not in d.keys()):
d[word[0]]=[]
d[word[0]].append(word)
else:
if(word not in d[word[0]]):
d[word[0]].append(word)
for k,v in d.items():
print(k,":",v)

49 Write a program in python using dictionary to print the name and salary of
employee.

Ans:
names=[]
dept=[]
salary=[]
details={'Emp Name':names,'Department':dept,'Salary':salary}
rec=int(input('How many records U want to insert'))
for i in range(rec):
n=input('Enter Emp name::')
names.append(n)
d=input('Enetr Emp Department::')
dept.append(d)
s=int(input('Enetr Emp salary::'))
salary.append(s)

14 | P a g e
Visit Python4csip.com for more updates

for key,val in details.items():


print(key, "=>", val)
50 Write a program to count the frequency of each character in string using
dictionary.
Ans:

test_str=input('enter any string\n')


all_freq = {}
for i in test_str:
if i in all_freq:
all_freq[i] += 1
else:
all_freq[i] = 1
print ((all_freq))

15 | P a g e

You might also like