What will be the output of the following Python code snippet?
d1 = {"john":40, "peter":45}
d2 = {"john":466, "peter":45}
d1 == d2
True
False
None
Error
d1 > d2
d = {"john":40, "peter":45}
d["john"]
40
45
“john”
“peter”
d.delete(“john”:40)
d.delete(“john”)
del d[“john”]
del d(“john”:40)
d.size()
len(d)
size(d)
d.len()
print(list(d.keys()))
[“john”, “peter”]
[“john”:40, “peter”:45]
(“john”, “peter”)
(“john”:40, “peter”:45)
Since “susan” is not a value in the set, Python raises a KeyError exception
It is executed fine and no exception is raised, and it returns None
Since “susan” is not a key in the set, Python raises a KeyError exception
Since “susan” is not a key in the set, Python raises a syntax error
The values of a dictionary can be accessed using keys
The keys of a dictionary can be accessed using values
Dictionaries aren’t ordered
Dictionaries are mutable
{1: ‘A’, 2: ‘B’}
dict([[1,”A”],[2,”B”]])
{1,”A”,2”B”}
{ }
a={1:"A",2:"B",3:"C"}
for i,j in a.items():
print(i,j,end=" ")
1 A 2 B 3 C
1 2 3
A B C
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’}
[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=" ")
‘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’)])