NANDHOO
.
Learning Paths
Dashboard
Sign In
Home
Dashboard
Python Programming: From Beginner to Production
Loops & Comprehensions
Quiz
Chapter Quiz: Loops & Comprehensions
Loops & Comprehensions Quiz
Complete this assessment with 100% score to master this chapter.
01
What does enumerate() do when used in a for loop?
It counts the total items in a list
It provides both the index and the value for each item
It sorts the items before looping
It skips items with None values
02
What does this list comprehension produce: [x**2 for x in range(1, 6)]?
[1, 2, 3, 4, 5]
[1, 4, 9, 16, 25]
[2, 4, 6, 8, 10]
[0, 1, 4, 9, 16]
03
What is the difference between a list comprehension and a generator expression?
There is no difference
List comprehensions use () and generators use []
List comprehensions build the whole list in memory; generators produce values one at a time
Generators are always faster for small lists
04
What does zip() do when given two lists of different lengths?
It raises an error
It pads the shorter list with None
It stops at the end of the shortest list
It repeats the shorter list to match
05
How do you write a dictionary comprehension that maps words to their lengths?
[word: len(word) for word in words]
{word: len(word) for word in words}
(word: len(word) for word in words)
dict(word=len(word) for word in words)
Submit Answers
Back to Lesson
Next Chapter