DEV Community

🤔 Python Quiz: Unpacking Sequences in Python

Vladimir Ignatev on December 06, 2023

Follow me to learn 🐍 Python in 5-minute a day fun quizzes! Todays quiz is all about sequences and unpacking them. When you deal with list-type obj...
Collapse
 
vladignatyev profile image
Vladimir Ignatev

The correct answer is Sample 1.

In Sample 1, the function process_data uses the unpacking feature of Python to neatly extract a name, an age, and a list of scores from the given arguments. The syntax name, age, *scores = data correctly assigns the first two elements of the data to name and age, and the rest of the elements to scores as a list.

Sample 2, however, incorrectly attempts to unpack the data.

This quiz is based on Python's sequence unpacking, which allows for a flexible and readable way to assign values from sequences (like lists or tuples) to variables. This feature is covered in various Python Enhancement Proposals (PEPs) related to tuple unpacking and extended iterable unpacking, such as PEP 3132.