Explore hidden Python behaviors and backend development tricks with this quiz designed to uncover little-known facts, quirks, and features of Python. Perfect for backend developers seeking to deepen their Python knowledge with fascinating details and surprising capabilities.
What surprising behavior occurs when a mutable default argument like a list is used in a Python function definition?
Explanation: If you use a mutable default argument, such as a list or dictionary, the same object is shared among all calls that omit that argument. This can lead to unexpected results like accumulated items across calls. Choosing a new list isn't automatic; that's why 'A new list is created for every call' is incorrect. No runtime error occurs, making that distractor wrong. Finally, it's possible—though not recommended—to use mutable defaults, so Python does not enforce immutability for default arguments.
Which statement is true about tuples containing mutable items in Python?
Explanation: Tuples themselves are immutable, meaning the container cannot change, but the objects they reference may still be changed if they are mutable, like lists. The statement about all tuple contents becoming immutable is inaccurate; Python never forces underlying objects to be immutable just because they're inside a tuple. Tuples can hold any objects, including lists and dictionaries, so stating they only hold integers and strings is false. Tuples themselves never become mutable, regardless of their contents.
How does Python's starred expression (*) behave when unpacking values from a list into variables?
Explanation: The starred expression collects all surplus values during unpacking into a list assigned to the starred variable, enabling flexible assignments like a, *b, c = [1, 2, 3, 4]. It works with lists, not just dictionaries, so the second option is incorrect. The starred expression doesn't force variables to become strings, nor does it throw errors when used outside a loop, making the third and fourth options wrong.
What unique optimization does Python perform with small integer values like 0 to 256?
Explanation: Python reuses memory objects for small integers in a specific range, typically from 0 to 256, meaning identical references point to the same object. This speeds up integer operations and saves memory. Larger integers do not have this behavior, making the second statement incorrect. Python does not convert these integers into floats, and the idea of no special handling is also inaccurate.
Which lesser-known fact is true about the bool type in Python related to integers?
Explanation: In Python, the bool type is actually a subclass of int, so True and False can participate in arithmetic, behaving as 1 and 0. They can be used where integers are needed, so the second option is incorrect. It's not unrelated to numbers, and bool values are never stored as strings, making the last two options incorrect.
Given a set containing one list as an element, what happens in Python?
Explanation: Python sets require their elements to be hashable, and lists are mutable and thus unhashable, causing a TypeError if you try to add a list to a set. Lists can't be used as set elements, so the second option is wrong. The set doesn't convert to a tuple, and sets never allow duplicate entries, so the remaining distractors are incorrect.
Starting with Python 3.7, what guarantee is made regarding the order of keys in a dictionary?
Explanation: Starting from Python 3.7, dictionaries preserve the insertion order of keys, making data processing more predictable. Prior versions made no such guarantee. Keys are not shuffled; hashing is used internally but doesn't dictate user-visible order. Dictionaries do not sort keys alphabetically unless you explicitly request it.
Which feature does Python offer when evaluating expressions like 1 u003C x u003C 5?
Explanation: Python evaluates chained comparisons as you'd expect from mathematical notation, ensuring all comparisons are checked in a single logical statement. It does not stop after the first check, nor does it invoke a nonexistent __compare__ method. Chained comparisons are perfectly legal Python syntax, so no syntax error occurs.
When comparing None to other types using greater-than or less-than operators, what happens in Python 3?
Explanation: Under Python 3, comparing None to other types using u003C or u003E leads to a TypeError, enforcing stricter type rules. In earlier Python versions, None was sometimes treated as less than other types, but that's no longer the case. None isn't considered greater than others, nor is it converted to zero in comparisons; both of which would misrepresent Python's actual behavior.
What special property do dict_keys and dict_values views have when you modify the original dictionary?
Explanation: Dictionary views such as keys and values update in real time to reflect changes in the original dictionary, letting you monitor the dictionary's contents as they evolve. They are not static snapshots, hence option two is wrong. Views remain views and do not auto-convert to lists; also, they are not cleared on updates, making the last option incorrect as well.