Are you ready?

1 questions to solve.

Instructions
  1. This quiz goes to full-screen once you press the Start button, or any Next button after exiting the quiz window.
  2. At the end of the quiz, you are able to review all the questions that you answered wrong and see their explanations.
What does the following list comprehension produce?
[0 for i in range(5)]
In each iteration over the sequence range(5), the value 0 is pushed onto the list, ultimately giving [0, 0, 0, 0, 0]. Hence the correct choice is (B). For more details, refer to Python List Comprehensions.
What is the value of l in the code below?
l = [i * i for i in range(3)]
For each iteration over the sequence range(3) (i.e 0, 1, 2), the square of the current element is pushed onto the list, ultimately giving [0, 1, 4]. This goes with choice (B). For more details, refer to Python List Comprehensions.
What is the value of l in the code below?
l = [0 if x % 2 == 0 else x for x in range(10)]
For each element in the sequence range(10) (i.e 0, 1, 2, 3, 4, 5, 6, 7, 8, 9), if it's divisible by 2, 0 is pushed onto the list, or else the element itself. This leads to the list [0, 1, 0, 3, 0, 5, 0, 7, 0, 9], which goes with choice (B). For more details, refer to Python List Comprehensions — conditional expressions.
What does the following code print?
l1 = [0 for i in range(10)]
l2 = [0 if x % 2 == 0 else x for x in range(0, 20, 2)]

print(l1 == l2)
The first comprehension iterates over the sequence range(10) and pushes 0 in each iteration, thus giving a list of 10 0s. The second comprehension iterates over the sequence range(0, 20, 2) (the first 10 non-negative evens) and pushes 0 if the current element is divisible by 2, or else x. Since, all elements are divisible by 2, every element in the list is 0. This list also consists of 10 0's.

Hence, these two lists are equal to one another, which goes with choice (A). For more details, refer to Python List Comprehensions — conditional expressions.
What does the following code print?
s = 'Hello World!'
l = [_s.upper() for _s in s[:-1]]

print(l)
The comprehension iterates over s[:-1], which is the string 'Hello World', and pushes each character's uppercased version onto the list. This yields ['H', 'E', 'L', 'L', 'O', ' ', 'W', 'O', 'R', 'L', 'D'].

Hence, the correct choice is (A). For more details, refer to Python List Comprehensions — function calls.
What does the following code print?
words = ['Bye', 'OK', 'Savage', 'Love', 'Calibrate', 'Height', 'Linguistic']

print([word for word in words if len(word) <= 3])
The comprehension iterates over words and pushes only those elements whose length is lesser than or equal to 3. This gives the list ['Bye', 'OK'], which goes with choice (A). For more details, refer to Python List Comprehensions — the if clause.
What does the following code print?
nums = [0, -5, 3, -9, -6, -11, 0, 0, 5, 3, 27]

print([num for num in nums if num % 3 == 0][:-2])
The comprehension iterates over nums and pushes all those elements that are divisible by 3. This gives the list [0, 3, -9, -6, 0, 0, 3, 27]. Next, this list is sliced from the beginning to the third-last element, giving [0, 3, -9, -6, 0, 0].

Hence the correct choice is (B). For more details, refer to Python List Comprehensions — the if clause.
What is the value of l in the code below?
l = [i + j for i in range(2) for j in range(5)]
Each element in the sequence range(2) (i.e 0, 1) is added one-by-one to each element in the sequence range(5) (i.e 0, 1, 2, 3, 4). This gives [0, 1, 2, 3, 4, 1, 2, 3, 4, 5], which goes with choice (A). For more details, refer to Python List Comprehensions — nested loops.
What is the value of l in the code below?
l = [i for i in [i for i in range(5)][:-4]]
First, the inner comprehension [i for i in range(5)] is evaluated. It's simply the list [0, 1, 2, 3, 4]. Next, this list is sliced from the beginning to the first character which merely gives [0]. The outer comprehension iterates over this list [0], and pushes each element onto the main list, once again, merely giving [0].

Hence, the correct choice is (A). For more details, refer to Python List Comprehensions.
What is the value of l in the code below?
l = [[i for j in range(2)] for i in range(3)]
For each element in the sequence range(3), the comprehension [i for j in range(2)] is evaluated and pushed onto the main list. [i for j in range(2)] simply iterates over range(2) and pushes i for each element, thus giving [i, i]. So, for each iteration of the main comprehension, [i, i] is pushed onto the main list. This gives [[0, 0], [1, 1], [2, 2]].

Hence, the correct choice is (B). For more details, refer to Python List Comprehensions — 2D lists.