100% found this document useful (1 vote)
2K views

1 What Will Be The Output of Following Code-: A (1:"A",2:"B",3:"C") Foriina: Print (I, End " ")

The document contains code snippets and their expected outputs related to dictionaries in Python. Some key points covered include: - Printing dictionary keys and values using for loops - Creating dictionaries using dictionary comprehensions - Adding, updating, and deleting dictionary keys and values - Built-in dictionary methods like pop(), copy(), items(), etc. - Type of keys allowed in dictionaries - Iterating over dictionaries

Uploaded by

Ayaan Ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
2K views

1 What Will Be The Output of Following Code-: A (1:"A",2:"B",3:"C") Foriina: Print (I, End " ")

The document contains code snippets and their expected outputs related to dictionaries in Python. Some key points covered include: - Printing dictionary keys and values using for loops - Creating dictionaries using dictionary comprehensions - Adding, updating, and deleting dictionary keys and values - Built-in dictionary methods like pop(), copy(), items(), etc. - Type of keys allowed in dictionaries - Iterating over dictionaries

Uploaded by

Ayaan Ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

1 What will be the output of following code-

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

Ans: 1 2 3

2 What will be the output of following code-


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

Ans{0: 'Hi!0', 1: 'Hi!1', 2: 'Hi!2', 3: 'Hi!3', 4: 'Hi!4'}

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)

Ans:{0:1,1:1,2:1}
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}

5 What will be the output of following code-


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

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))

Ans:
2

7 Predict the Output:

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


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

Ans:ERROR
8 What will be the output of following program: dict1
= {"key1":1, "key2":2}
dict2 = {"key2":2, "key1":1}
print(dict1 == dict2)

Ans:TRUE

9 What will be the output of following program:


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

Ans:{'Virat':

1, 'Rohit': 2,

'Rahul': 2}

10 What will be the output of following program:


a=dict()
a[1]

Ans:ERROR
11 What will be the output of following program: a={}
a.fromkeys([1,2,3],"check")

Ans:{}

12 What will be the output of following program: a={}


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

Ans:{'a': 1, 'b': [2, 3, 4]}

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:

count += a[i]
print(count)

Ans: 4
6
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)

Ans:2

15 What will be the output of following program: test =


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

Ans:0

16 What will be the output of following program:


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

Ans:>>>

17 What will be the output of following program:


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

Ans:1 2 3
18 What will be the output of following program:
a={1:5,2:3,3:4}
a.pop(3)
print(a)

Ans:{1: 5, 2: 3}

19 What will be the output of following program:


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

Ans: 9

20 What will be the output of following program:


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

Ans:{1: 'A', 2: 'B', 3: 'C', 4: 'D'}

21 What will be the output of following program:


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

Ans: C
22 What will be the output of following program:
a={1:"A",2:"B",3:"C"}
b=a.copy()
b[2]="D"
print(a)

Ans:{1: 'A', 2: 'B', 3: 'C'}

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)

Ans: 2
4

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]))
Ans:1

25 What will be the output of following program: dict


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

Ans:96
98
97

26 What will be the output of following program: rec =


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

Ans: FALSE

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)

Ans: TRUE
28 What will be the output of following program:
my_dict = {}
my_dict[(1,2,4)] = 8
my_dict[(4,2,1)] = 10
my_dict[(1,2)] = 12
sum = 0
for k in my_dict: sum
+= my_dict[k]
print (sum)
print(my_dict)

Ans: {(1, 2, 4): 8, (4, 2, 1): 10, (1, 2): 12}

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)

Ans: 6
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)

Ans: 4

31 What will be the output of following program: a =


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

Ans: ERROR

32 What will be the output of following program: a =


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

Ans 1:
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="#")

Ans: 1 a#2 b#3 c#

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)

Ans: {1:3,2:1,3:1,5:2,6:2}

35 What will be the output of following program:


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

Ans:Sharma
Ishant
Rohit
{1: 'Rohit', 2: 'Sharma', 'name': 'Ishant', 4: 'Sharma'}

36 What will be the output of following program:


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

Ans:dict_items([('Name', 'Rohit'), ('Age', 30)])

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


Ans:
d = {'a': 'juice', 'b': 'grill', 'c': 'corn'}

for key, value in d.items():


print(key, value)
38 Write a Python script to merge two Python dictionaries Ans:

dict_1 = {1: 'a', 2: 'b'}


dict_2 = {2: 'c', 4: 'd'}

print({**dict_1, **dict_2})

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

Ans:dictionary = {"a": 5, "b": 2, "c": 8}


min_key = min(dictionary, key=dictionary.get)
print(min_key)
print(dictionary.get(min_key))
40 Write a Python program to multiply all the items in a dictionary

Ans: dict = {'data1':100,'data2':-54,'data3':247}


result=1
for key in dict:
result=result * _dict[key]

print(result)

41 Write a Python program to remove duplicates from Dictionary

Ans:
42 Write a Python program to sum all the items in a dictionary.

Ans: my_dict = {'data1':100,'data2':-54,'data3':247}

print(sum(my_dict.values()))

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy