What is the key characteristic of an anonymous or lambda function in Python?

Correct answer: It consists of a single statement, the result of which is the return value.

Explanation

This question tests the definition and primary constraint of lambda functions in Python. Their main purpose is to create small, inline functions without the full `def` syntax, which is particularly useful in data analysis for operations like sorting or mapping.

Other questions

Question 1

What is the primary characteristic of a Python tuple as a data structure?

Question 2

What is the result of the following Python code snippet? tup = tuple(['foo', [1, 2], True]); tup[1].append(3)

Question 3

In Python, what does the special syntax `*rest` do when unpacking a tuple or list?

Question 4

What is a key difference between the `extend` method and using the `+` operator for combining lists?

Question 5

When using slice notation on a sequence like `seq[start:stop]`, which element is not included in the result?

Question 6

What is the output of the slice notation `seq[::-1]` on a list `seq`?

Question 7

In the context of Python dictionaries, what is the concept of 'hashability' related to?

Question 8

What is the purpose of the `setdefault` dictionary method?

Question 9

How can you create a set from the list `[2, 2, 2, 1, 3, 3]` in Python?

Question 10

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)`?

Question 11

What does the built-in Python function `enumerate` return when used on a collection?

Question 12

When using the `zip` function on multiple sequences of different lengths, what determines the number of tuples produced?

Question 13

What is the purpose of the `reversed` function in Python?

Question 14

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"]`?

Question 15

What is the primary syntactical difference between a list comprehension and a set comprehension?

Question 16

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]`?

Question 17

What value is returned by a Python function that reaches its end without an explicit `return` statement?

Question 18

When a function returns multiple values, such as `return a, b, c`, what data structure is actually being returned?

Question 20

How does a generator function differ from a normal function in Python?

Question 21

What is the syntax for creating a generator expression in Python?

Question 22

What is the primary function of the `itertools.groupby` generator?

Question 23

In a `try...except...finally` block, when is the code inside the `finally` clause executed?

Question 24

What is the primary advantage of using the `with` statement when working with files (e.g., `with open(...) as f:`)?

Question 25

What does the file open mode 'x' do in Python?

Question 26

What is the difference between opening a file in text mode (e.g., 'r') versus binary mode (e.g., 'rb')?

Question 27

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?

Question 28

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?

Question 29

Consider the code `b = ["saw", "small", "He", "foxes", "six"]; b.sort(key=len)`. What is the final state of the list `b`?

Question 30

What is the primary difference between a list's `sort()` method and Python's built-in `sorted()` function?

Question 31

Given `tuples = zip(range(5), reversed(range(5)))`, what will be the result of `dict(tuples)`?

Question 32

What is the function of the `collections.defaultdict` class?

Question 33

If `c = a.copy()` and `a` and `b` are sets, what is the effect of the in-place operation `c |= b`?

Question 34

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)]`?

Question 35

In a Python function definition like `def my_func(x, y, z=1.5):`, what is `z=1.5` an example of?

Question 36

What does the `global` keyword do inside a Python function?

Question 37

Why is it possible to pass functions as arguments to other functions in Python?

Question 38

In a `try...except...else` block, when does the `else` block execute?

Question 39

To catch multiple specific exception types in a single `except` block, what syntax should be used?

Question 40

What does the `list` built-in function do when applied to a generator expression?

Question 41

If `seq = [7, 2, 5, 1, 3]`, what is the result of `a = sorted(seq)`?

Question 42

What happens when you use the `pop` method on a dictionary, like `d.pop("key")`?

Question 43

Which method is used to merge one dictionary into another, modifying the first dictionary in-place?

Question 44

What is the result of using the `*` operator on a tuple, as in `('foo', 'bar') * 4`?

Question 45

When unpacking a tuple using `a, b, *_ = values`, what is the purpose of the underscore `_` variable?

Question 46

What is the computational cost of the list `insert` method compared to `append`?

Question 47

When checking if a value is in a list versus a set, which is typically faster for large collections and why?

Question 48

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]`?

Question 49

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?

Question 50

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`?