Python's Secret Features: Little-Known Backend Facts Quiz Quiz

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.

  1. Mutable Default Arguments

    What surprising behavior occurs when a mutable default argument like a list is used in a Python function definition?

    1. Default arguments must always be immutable.
    2. The default list is shared across all function calls.
    3. A new list is created for every call.
    4. The function throws a runtime error.

    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.

  2. Tuple Immutability and Containing Objects

    Which statement is true about tuples containing mutable items in Python?

    1. A tuple's immutability does not prevent its mutable items from being changed.
    2. Tuples become mutable if they hold a mutable object.
    3. All objects inside a tuple become automatically immutable.
    4. Tuples can only contain integers and strings.

    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.

  3. Unpacking Unique Syntax

    How does Python's starred expression (*) behave when unpacking values from a list into variables?

    1. It throws an error if used outside a loop.
    2. It assigns all remaining values as a list to the starred variable.
    3. It only works with dictionaries.
    4. It converts all the variables to strings.

    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.

  4. Integer Identity for Small Numbers

    What unique optimization does Python perform with small integer values like 0 to 256?

    1. It turns them into floating-point numbers.
    2. It stores every integer as a separate object.
    3. No special handling is applied to small integers.
    4. It reuses the same object in memory for these integers.

    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.

  5. Boolean is a Subclass

    Which lesser-known fact is true about the bool type in Python related to integers?

    1. bool is a subclass of int and behaves similarly in arithmetic.
    2. bool can never be used where an int is required.
    3. bool is unrelated to numbers in Python.
    4. bool variables are always stored as strings.

    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.

  6. Set Uniqueness and List Membership

    Given a set containing one list as an element, what happens in Python?

    1. No error, but duplicates are allowed in sets.
    2. The set is automatically converted to a tuple.
    3. A TypeError occurs because lists are unhashable.
    4. The list element is stored normally in the set.

    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.

  7. Dictionary Key Order Guarantee

    Starting with Python 3.7, what guarantee is made regarding the order of keys in a dictionary?

    1. Keys are always sorted alphabetically.
    2. Keys are randomly shuffled on access.
    3. The order of insertion is preserved.
    4. Order depends on the hash value of the keys.

    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.

  8. Chained Comparison Magic

    Which feature does Python offer when evaluating expressions like 1 u003C x u003C 5?

    1. It performs only the first comparison and ignores the rest.
    2. It throws a syntax error for chained comparisons.
    3. It checks that x is greater than 1 and less than 5 in a single step.
    4. It calls the __compare__ method on x.

    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.

  9. NoneType Comparisons

    When comparing None to other types using greater-than or less-than operators, what happens in Python 3?

    1. None is always considered less than other types.
    2. A TypeError is raised.
    3. None is always considered greater than other types.
    4. None is silently converted to zero.

    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.

  10. Dictionary Views Dynamic Update

    What special property do dict_keys and dict_values views have when you modify the original dictionary?

    1. They reflect updates made to the dictionary dynamically.
    2. They are fixed snapshots and never change.
    3. They automatically convert to lists after updates.
    4. They are cleared each time the dictionary changes.

    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.