Recall & Review
beginner
What does the
enumerate() function do in Python?It adds a counter to an iterable (like a list) and returns it as an enumerate object, which yields pairs of (index, item).
Click to reveal answer
beginner
How do you use
enumerate() with a list fruits = ['apple', 'banana', 'cherry'] to get index and fruit?Use a for loop:
for index, fruit in enumerate(fruits):
print(index, fruit)Click to reveal answer
intermediate
What is the purpose of the optional
start parameter in enumerate()?It sets the starting index number. By default, it starts at 0, but you can change it to any integer.
Click to reveal answer
intermediate
What type of object does
enumerate() return?It returns an
enumerate object, which is an iterator that produces pairs of (index, item) when looped over.Click to reveal answer
beginner
Why is
enumerate() better than using a manual counter variable in a loop?Because it is cleaner, less error-prone, and makes the code easier to read by automatically handling the index counting.
Click to reveal answer
What does
enumerate(['a', 'b', 'c']) produce when converted to a list?✗ Incorrect
The default start index is 0, so enumerate pairs each item with its index starting at 0.
How can you start enumeration at 1 instead of 0?
✗ Incorrect
The
start parameter sets the starting index, so use enumerate(iterable, start=1).Which of these is a correct way to get index and value from a list
nums?✗ Incorrect
The enumerate function returns (index, value) pairs, so unpack as
i, val.What type of object is returned by
enumerate()?✗ Incorrect
enumerate() returns an iterator object that yields index-item pairs.Why might you prefer
enumerate() over a manual counter variable?✗ Incorrect
enumerate() simplifies code by managing the index count automatically.Explain how the
enumerate() function works and give an example of its use.Think about how you get both position and value when looping.
You got /3 concepts.
Describe the purpose of the
start parameter in enumerate() and how to use it.It lets you choose where counting begins.
You got /3 concepts.