Variable Parameters (*)
Variable parameters, as the name implies, its parameters are variable, such as lists, dictionaries and so On. If we need a function to handle a variable number of parameters, we can use variable Parameters.
When we look at a lot of Python source code, we often see a function (* parameter 1, * * parameter 2) such a function definition, the * parameter and the * * parameter is a variable parameter, for a moment will be a bit confusing. In fact, as long as the definition of variable parameters of the function is clear, it is not difficult to understand.
When we don't know how to define a function with a few parameters, variable parameters can be Used.
In python, the parameter with * is used to accept a variable number of Parameters.
If a function is defined as Follows:
def functiontest (*args): ... . .... ....
Called when we can call This:
FunctionTest (1) or functiontest( or functiontest)
You can pass in multiple parameters Later.
Take a look at the example code and see how it applies:
def get_sum (*numbers): = 0 for in numbers: + = N return sum # write down your code here to call get_sum for 5 numbers and output this result Print (get_sum (1,2,3,4,5))
The result would be?
more Learning content, on the code Bud net, Http://www.mayacoder.com/lesson/index
Python Beginner Learning Basics function-variable Parameters *