Question 26
What is the type of the expression x = (5) vs y = (5,)?
In Python, parentheses without a trailing comma (5) evaluate to an integer grouping. A single-element tuple requires a trailing comma (5,).
What is the type of the expression x = (5) vs y = (5,)?
In Python, parentheses without a trailing comma (5) evaluate to an integer grouping. A single-element tuple requires a trailing comma (5,).
What is the output of the following dictionary lookup?
d = {1: 'a', True: 'b', 1.0: 'c'}
print(len(d))In Python, 1, True, and 1.0 all evaluate as equal (1 == True == 1.0) and have the same hash value. Thus they overwrite the same dictionary key. Final length is 1.
Which of the following objects CANNOT be used as a key in a standard Python dictionary?
Dictionary keys must be hashable (immutable). A Python list [1, 2, 3] is mutable and unhashable, raising TypeError: unhashable type: 'list'.
Evaluate the printed integer output:
d = {'a': 10, 'b': 20}
print(d.get('c', 30) + d.get('a', 0))d.get('c', 30) key 'c' is absent, so returns default 30. d.get('a', 0) key 'a' exists, returns 10. 30 + 10 = 40.
What will be the output of the following set operation?
s1 = {1, 2, 3, 4}
s2 = {3, 4, 5, 6}
print(s1 ^ s2)The ^ operator computes the symmetric difference (elements in either set but not in both). Intersections {3, 4} are excluded, leaving {1, 2, 5, 6}.