December 2018
Beginner to intermediate
796 pages
19h 54m
English
Sometimes you may want to pass a variable number of positional arguments to a function, and Python provides you with the ability to do it. Let's look at a very common use case, the minimum function. This is a function that calculates the minimum of its input values:
# arguments.variable.positional.pydef minimum(*n): # print(type(n)) # n is a tuple if n: # explained after the code mn = n[0] for value in n[1:]: if value < mn: mn = value print(mn)minimum(1, 3, -7, 9) # n = (1, 3, -7, 9) - prints: -7minimum() # n = () - prints: nothing
As you can see, when we specify a parameter prepending a * to its name, we are telling Python that that parameter will be collecting a variable number of positional arguments, according ...
Read now
Unlock full access