What will be the output of the following Python code snippet?
a={1:"A",2:"B",3:"C"} print(a.get(1,4))
1
A
4
Invalid syntax for get method
a={1:"A",2:"B",3:"C"} print(a.get(3))
Error, invalid syntax
5
C
a={1:"A",2:"B",3:"C"} a.setdefault(4,"D") print(a)
{1: ‘A’, 2: ‘B’, 3: ‘C’, 4: ‘D’}
None
Error
[1,3,6,10]
What will be the output of the following Python code?
a={1:"A",2:"B",3:"C"} b={4:"D",5:"E"} a.update(b) print(a)
{1: ‘A’, 2: ‘B’, 3: ‘C’}
Method update() doesn’t exist for dictionaries
{1: 'A', 2: 'B', 3: 'C', 4: 'D', 5: 'E'}
{4: ‘D’, 5: ‘E’}
a={1:"A",2:"B",3:"C"} b=a.copy() b[2]="D" print(a)
Error, copy() method doesn’t exist for dictionaries
{1: ‘A’, 2: ‘D’, 3: ‘C’}
“None” is printed
a={1:"A",2:"B",3:"C"} a.clear() print(a)
{ None:None, None:None, None:None}
{1:None, 2:None, 3:None}
{ }
More than one key isn’t allowed
Keys must be immutable
Keys must be integers
When duplicate keys encountered, the last assignment wins
a={1:5,2:3,3:4} a.pop(3) print(a)
{1: 5}
{1: 5, 2: 3}
Error, syntax error for pop() method
{1: 5, 3: 4}
a={1:"A",2:"B",3:"C"} for i in a: print(i,end=" ")
1 2 3
‘A’ ‘B’ ‘C’
1 ‘A’ 2 ‘B’ 3 ‘C’
Error, it should be: for i in a.items():
a={1:"A",2:"B",3:"C"} print(a.items())
Syntax error
dict_items([(‘A’), (‘B’), (‘C’)])
dict_items([(1,2,3)])
dict_items([(1, ‘A’), (2, ‘B’), (3, ‘C’)])
More than one key can have the same value
The values of the dictionary can be accessed as dict[key]
Values of a dictionary must be unique
Values of a dictionary can be a mixture of letters and numbers
>>> a={1:"A",2:"B",3:"C"} >>> del a
method del doesn’t exist for the dictionary
del deletes the values in the dictionary
del deletes the entire dictionary
del deletes the keys in the dictionary
Removes an arbitrary element
Removes all the key-value pairs
Removes the key-value pair for the key given as an argument
Invalid method for dictionary
total={} def insert(items): if items in total: total[items] += 1 else: total[items] = 1 insert('Apple') insert('Ball') insert('Apple') print (len(total))
3
2
0
a = {} a[1] = 1 a['1'] = 2 a[1]=a[1]+1 count = 0 for i in a: count += a[i] print(count)
Error, the keys can’t be a mixture of letters and numbers
numbers = {} letters = {} comb = {} numbers[1] = 56 numbers[3] = 7 letters[4] = 'B' comb['Numbers'] = numbers comb['Letters'] = letters print(comb)
Error, dictionary in a dictionary can’t exist
‘Numbers’: {1: 56, 3: 7}
{‘Numbers’: {1: 56}, ‘Letters’: {4: ‘B’}}
{‘Numbers’: {1: 56, 3: 7}, ‘Letters’: {4: ‘B’}}
test = {1:'A', 2:'B', 3:'C'} test = {} print(len(test))
An exception is thrown
test = {1:'A', 2:'B', 3:'C'} del test[1] test[1] = 'D' del test[2] print(len(test))
Error as the key-value pair of 1:’A’ is already deleted
a = {} a[1] = 1 a['1'] = 2 a[1.0]=4 count = 0 for i in a: count += a[i] print(count) print(a)
6
a={} a['a']=1 a['b']=[2,3,4] print(a)
Exception is thrown
{‘b’: [2], ‘a’: 1}
{‘b’: [2], ‘a’: [3]}
{'a': 1, 'b': [2, 3, 4]}