Puzzles 23 to 26
Explore Python 3 puzzles that cover slice assignments, default function arguments, using len() on slices, and working with nested lists. You will understand how sequence modifications affect data, how to use default parameters in functions, and how to access elements in complex list structures. This lesson helps build essential practical skills for Python beginners.
Puzzle 23
What is the output of the following code?
Note: Enter the output you guessed. If your guessed output is a list, enter each element of the list separated by a comma. Then, press the Run button, and your new Elo will be calculated. Don’t forget to save your Elo rating.
Slice assignment
Slicing helps you to read specific subsequences. Moreover, slice assignments enable you to replace, append, or clear whole subsequences.
In the puzzle, we have a list of customers that are partially replaced by new customers. The puzzle shows how the length of the original sequence may change due to the slice assignment. The slice assignment inserts a list of three customers into the customer list.
A beautiful way to clear the list is:
customers[:] = []
Solution
The correct solution is:
['Marie', 'Anne', 'Barack', 'Olivia', 'Sophia']
Your Elo rating will be updated according to the following table.
| Your Elo | Correct | Incorrect |
|---|