3. Consider the following code fragments. What outputs will they produce?
(1) aDict={'Bhavna’:1, ‘Richard”:2, ‘Firoza”:3, 'Arshnoor":4}
temp=0
for value in aDict.values();
temp=temp-value
print(temp
Answers
Answer:
How are dictionaries different from lists? Lists are sequential collections(ordered) and dictionaries are non-sequential collections(unordered). Q2. Consider the following code fragments. What outputs will they produce? (i) adict = { Bhavna : 1, Richards : 2, Firoza : 10, Arshnoor : 20 } temp = 0 for value in adict.values( ) : temp = temp + value print(temp) (ii) adict = { Bhavna : 1, Richards : 2, Firoza : 10, Arshnoor : 20 } temp = 0 for key in adict : if temp < key : temp = key print(temp) (iii) adict = { Bhavna : 1, Richards : 2, Firoza : 10, Arshnoor : 20 } k = Bhavna v = -1 if k in adict : adict[k] = v print(adict) Sol2. (i) 33 (ii) Richard (iii) { Bhavna : -1, Richards : 2, Firoza : 10, Arshnoor : 20 } Q3. Write a Python program that takes a value and checks whether the given value is part of given dictionary or not. If it is, it should print the corresponding key otherwise print an error message. Sol3. dict1= {0: Zero, 1: one, 2: Two, 3: Three, 4: Four, 5: Five } val = input( Enter Value: ) for k in dict1 : if dict1[k] == val : print( exist at, k) else : print( Not Found ) Q4. Why is a dictionary termed as an unordered collection of objects? Q5. What type of objects can be used as keys in dictionaries? Q6. In no more than one sentence, explain the following Python error and how it could arise: P a g e 7 9