ProgrammingHardIBM · Deloitte pattern
What is printed?
What is printed?
d = {"a": 1, "b": 2}
print({v: k for k, v in d.items()})- A. {'a': 1, 'b': 2}
- B. {1: 'a', 2: 'b'}
- C. {'1': 'a', '2': 'b'}
- D. KeyError
Correct answer: B. {1: 'a', 2: 'b'}
This comprehension swaps keys and values: the original values (1, 2) become the new dict's keys, and the original keys ('a','b') become the new values, giving {1: 'a', 2: 'b'}.
More Python Programming questions
- What is printed? python s1 = {1, 2, 3} s2 = {2, 3, 4} print(s1 - s2, s1 | s2, s1 & s2)
- What is printed? python print(all([1, 2, 3]), all([1, 0, 3]), any([0, 0, 0]), any([0, 1, 0]))
- What is printed? python x = None print(x or "default") y = 0 print(y or "default") z = "" print(z or "fallback")
- What is printed? python def gen(): try: x = yield 1 except GeneratorExit: print("closing") raise g = gen() next(g) g.close()
- What is printed? python a = 5 def outer(): x = 10 def inner(): nonlocal x x += a return x return inner() print(outer())
- What is printed? python class Temperature: def init(self, celsius=0): self.celsius = celsius @property def fahrenheit(self): return…