How does one `*args` parameter let a function accept any number of arguments the way `max` and `min` do — and why is looping over elements directly more Pythonic than `range(len(l))`?
Short drills on what this video just taught. Write the code, run the checks, and reveal the answer only if you are stuck.
Define a function product that accepts a variable number of arguments and returns
the product of all of them. Put a star before the parameter name so Python packs the
arguments into a tuple you can loop over.
It must work no matter how many numbers are passed — product(2, 3, 4), product(5),
and so on.
A *args parameter packing the arguments
State that Python collects whatever is passed into a tuple named args, and write a mean function that totals it and returns total / len(args), run on mean(1, 2, 3, 4, 5, 6) and mean(6, 0, 9).
Square-bracket syntax for lists
Record [] for the empty list and a one-element list written without a trailing comma, and note that the remaining operations match the tuple ones written with parentheses.
Direct iteration against range(len(l))
Write both versions of list_sum side by side, one indexing with square brackets over range(len(l)) and one with for i in l, listing the loop values 8, 3 and 5.
Adapting the loop body to string elements
Change total += e to total += len(s) for the list holding 'ab', 'def' and 'g', and record len(s) taking 2, then 3, then 1.