Why is it possible to pass functions as arguments to other functions in Python?
Explanation
This question tests the concept of functions as 'first-class citizens' in Python, meaning they can be treated like any other data type: assigned to variables, stored in collections, and passed as arguments. This enables powerful functional programming patterns.
Other questions
What is the primary characteristic of a Python tuple as a data structure?
What is the result of the following Python code snippet? tup = tuple(['foo', [1, 2], True]); tup[1].append(3)
In Python, what does the special syntax `*rest` do when unpacking a tuple or list?
What is a key difference between the `extend` method and using the `+` operator for combining lists?
When using slice notation on a sequence like `seq[start:stop]`, which element is not included in the result?
What is the output of the slice notation `seq[::-1]` on a list `seq`?
In the context of Python dictionaries, what is the concept of 'hashability' related to?
What is the purpose of the `setdefault` dictionary method?
How can you create a set from the list `[2, 2, 2, 1, 3, 3]` in Python?
Given two sets, `a = {1, 2, 3, 4, 5}` and `b = {3, 4, 5, 6, 7, 8}`, what is the result of `a.intersection(b)`?
What does the built-in Python function `enumerate` return when used on a collection?
When using the `zip` function on multiple sequences of different lengths, what determines the number of tuples produced?
What is the purpose of the `reversed` function in Python?
What 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"]`?
What is the primary syntactical difference between a list comprehension and a set comprehension?
Given 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]`?
What value is returned by a Python function that reaches its end without an explicit `return` statement?
When a function returns multiple values, such as `return a, b, c`, what data structure is actually being returned?
What is the key characteristic of an anonymous or lambda function in Python?
How does a generator function differ from a normal function in Python?
What is the syntax for creating a generator expression in Python?
What is the primary function of the `itertools.groupby` generator?
In a `try...except...finally` block, when is the code inside the `finally` clause executed?
What is the primary advantage of using the `with` statement when working with files (e.g., `with open(...) as f:`)?
What does the file open mode 'x' do in Python?
What is the difference between opening a file in text mode (e.g., 'r') versus binary mode (e.g., 'rb')?
If `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?
What is a potential issue when using the `seek()` method on a file opened in text mode with a multi-byte encoding like UTF-8?
Consider the code `b = ["saw", "small", "He", "foxes", "six"]; b.sort(key=len)`. What is the final state of the list `b`?
What is the primary difference between a list's `sort()` method and Python's built-in `sorted()` function?
Given `tuples = zip(range(5), reversed(range(5)))`, what will be the result of `dict(tuples)`?
What is the function of the `collections.defaultdict` class?
If `c = a.copy()` and `a` and `b` are sets, what is the effect of the in-place operation `c |= b`?
Why 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)]`?
In a Python function definition like `def my_func(x, y, z=1.5):`, what is `z=1.5` an example of?
What does the `global` keyword do inside a Python function?
In a `try...except...else` block, when does the `else` block execute?
To catch multiple specific exception types in a single `except` block, what syntax should be used?
What does the `list` built-in function do when applied to a generator expression?
If `seq = [7, 2, 5, 1, 3]`, what is the result of `a = sorted(seq)`?
What happens when you use the `pop` method on a dictionary, like `d.pop("key")`?
Which method is used to merge one dictionary into another, modifying the first dictionary in-place?
What is the result of using the `*` operator on a tuple, as in `('foo', 'bar') * 4`?
When unpacking a tuple using `a, b, *_ = values`, what is the purpose of the underscore `_` variable?
What is the computational cost of the list `insert` method compared to `append`?
When checking if a value is in a list versus a set, which is typically faster for large collections and why?
Given 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]`?
How 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?
What 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`?