Built-In Data Structures, Functions, and Files
50 questions available
Questions
What is the primary characteristic of a Python tuple as a data structure?
View answer and explanationWhat is the result of the following Python code snippet? tup = tuple(['foo', [1, 2], True]); tup[1].append(3)
View answer and explanationIn Python, what does the special syntax `*rest` do when unpacking a tuple or list?
View answer and explanationWhat is a key difference between the `extend` method and using the `+` operator for combining lists?
View answer and explanationWhen using slice notation on a sequence like `seq[start:stop]`, which element is not included in the result?
View answer and explanationWhat is the output of the slice notation `seq[::-1]` on a list `seq`?
View answer and explanationIn the context of Python dictionaries, what is the concept of 'hashability' related to?
View answer and explanationWhat is the purpose of the `setdefault` dictionary method?
View answer and explanationHow can you create a set from the list `[2, 2, 2, 1, 3, 3]` in Python?
View answer and explanationGiven two sets, `a = {1, 2, 3, 4, 5}` and `b = {3, 4, 5, 6, 7, 8}`, what is the result of `a.intersection(b)`?
View answer and explanationWhat does the built-in Python function `enumerate` return when used on a collection?
View answer and explanationWhen using the `zip` function on multiple sequences of different lengths, what determines the number of tuples produced?
View answer and explanationWhat is the purpose of the `reversed` function in Python?
View answer and explanationWhat is the result of the list comprehension `[x.upper() for x in strings if len(x) > 2]` where `strings` is `["a", "as", "bat", "car", "dove", "python"]`?
View answer and explanationWhat is the primary syntactical difference between a list comprehension and a set comprehension?
View answer and explanationGiven the nested list `some_tuples = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]`, what is the result of the nested list comprehension `[x for tup in some_tuples for x in tup]`?
View answer and explanationWhat value is returned by a Python function that reaches its end without an explicit `return` statement?
View answer and explanationWhen a function returns multiple values, such as `return a, b, c`, what data structure is actually being returned?
View answer and explanationWhat is the key characteristic of an anonymous or lambda function in Python?
View answer and explanationHow does a generator function differ from a normal function in Python?
View answer and explanationWhat is the syntax for creating a generator expression in Python?
View answer and explanationWhat is the primary function of the `itertools.groupby` generator?
View answer and explanationIn a `try...except...finally` block, when is the code inside the `finally` clause executed?
View answer and explanationWhat is the primary advantage of using the `with` statement when working with files (e.g., `with open(...) as f:`)?
View answer and explanationWhat does the file open mode 'x' do in Python?
View answer and explanationWhat is the difference between opening a file in text mode (e.g., 'r') versus binary mode (e.g., 'rb')?
View answer and explanationIf `f1 = open(path)` is opened in text mode and `f2 = open(path, mode='rb')` is opened in binary mode, and both call `read(10)`, why might `f1.tell()` and `f2.tell()` return different values?
View answer and explanationWhat is a potential issue when using the `seek()` method on a file opened in text mode with a multi-byte encoding like UTF-8?
View answer and explanationConsider the code `b = ["saw", "small", "He", "foxes", "six"]; b.sort(key=len)`. What is the final state of the list `b`?
View answer and explanationWhat is the primary difference between a list's `sort()` method and Python's built-in `sorted()` function?
View answer and explanationGiven `tuples = zip(range(5), reversed(range(5)))`, what will be the result of `dict(tuples)`?
View answer and explanationWhat is the function of the `collections.defaultdict` class?
View answer and explanationIf `c = a.copy()` and `a` and `b` are sets, what is the effect of the in-place operation `c |= b`?
View answer and explanationWhy might a programmer choose to use a generator expression `(x ** 2 for x in range(100))` instead of a list comprehension `[x ** 2 for x in range(100)]`?
View answer and explanationIn a Python function definition like `def my_func(x, y, z=1.5):`, what is `z=1.5` an example of?
View answer and explanationWhat does the `global` keyword do inside a Python function?
View answer and explanationWhy is it possible to pass functions as arguments to other functions in Python?
View answer and explanationIn a `try...except...else` block, when does the `else` block execute?
View answer and explanationTo catch multiple specific exception types in a single `except` block, what syntax should be used?
View answer and explanationWhat does the `list` built-in function do when applied to a generator expression?
View answer and explanationIf `seq = [7, 2, 5, 1, 3]`, what is the result of `a = sorted(seq)`?
View answer and explanationWhat happens when you use the `pop` method on a dictionary, like `d.pop("key")`?
View answer and explanationWhich method is used to merge one dictionary into another, modifying the first dictionary in-place?
View answer and explanationWhat is the result of using the `*` operator on a tuple, as in `('foo', 'bar') * 4`?
View answer and explanationWhen unpacking a tuple using `a, b, *_ = values`, what is the purpose of the underscore `_` variable?
View answer and explanationWhat is the computational cost of the list `insert` method compared to `append`?
View answer and explanationWhen checking if a value is in a list versus a set, which is typically faster for large collections and why?
View answer and explanationGiven the list of lists `all_data = [["John", "Emily"], ["Maria", "Natalia"]]`, what is the result of `[name for names in all_data for name in names if name.count("a") >= 2]`?
View answer and explanationHow can you unpack the tuple `values = (1, 2, 3, 4, 5)` so that variable `a` is 1, `b` is 2, and the rest of the values are discarded?
View answer and explanationWhat would the function `isiterable` defined in the text as `def isiterable(obj): try: iter(obj); return True; except TypeError: return False` return for the input `5`?
View answer and explanation