Like the C language, Python also has variable parameter functions, that is, a function can receive multiple parameters, and the number of these parameters is unknown before the function is called. The following article describes variable parameters in python.
Preface
Define functions in Python. you can use required parameters, default parameters, variable parameters, and keyword parameters. these four parameters can be used together or only use some of them, the order of parameter definitions must be: mandatory parameter, default parameter, variable parameter, and keyword parameter.
Variable parameters (*)
Variable parameters, as the name implies, are variable parameters, such as lists and dictionaries. If you need a function to process variable parameters, you can use variable parameters.
When we look at a lot of Python source code, we often see a function definition such as * parameter 1, ** parameter 2). This * parameter and ** parameter are variable parameters, it can be confusing for a moment. In fact, it is not difficult to understand the definition of variable parameters of a function.
When we don't know how many parameters need to be used to define a function, variable parameters can be used.
In Python, parameters with * are used to accept variable numbers of parameters.
If a function is defined as follows:
def functionTest(*args): .... .... ....
We can call this method as follows:
FunctionTest (1), functionTest (1, 2), or functionTest (1, 2, 3)
Multiple parameters can be input later.
Take a look at the instance code and observe * How to apply it:
Def get_sum (* numbers): sum = 0 for n in numbers: sum + = n return sum # write your code here to call get_sum to calculate the sum of the five numbers, and print (get_sum (, 5) the result ))
What is the result? You can start to understand all the variable parameters of functions in Python. I hope this article will help you learn or use python, if you have any questions, leave a message.
For more articles about variable parameters of functions in Python, refer to PHP!