Welcome to stationmaster Online Station master school study Python Knowledge, this paper studies in Pythonfunction Medium Variable parameters >. The main contents of this paper are: the definition of variable parameters, variable parameters with one asterisk * and variable parameters with two asterisks * *.
catalogue
1. Definition of variable parameters
2. Variable parameter with an asterisk *
3. Variable parameter with two asterisks * *
In the previous section, "in Python functions" Default parameters In the detailed explanation, it is mentioned that the default parameters cannot be set to variable objects. In this section, we will focus on variable parameters.
1. Definition of variable parameters
Variable parameters, also known as variable length parameters, are passed into the function Actual parameters It can be zero, one, two or even any one.
When defining variable parameters, there are two main forms: one is * with one asterisk, and the other is * * with two asterisks. The following are introduced respectively.
2. Variable parameter with an asterisk *
An asterisk: the function can receive any number of parameters. You only need to add an * (asterisk) in front of the formal parameter. The function of an asterisk formal parameter will put multiple parameters Position parameters Values are passed in as primitives, that is, multiple parameter values passed in can be traversed by primitives within the function.
For example, define a function so that it can receive multiple actual parameters, code As follows:
def kecheng(*jianzhan): # Define output Station construction Function of course "Print any incoming parameters" print ("\n The website building courses to learn are: ") for item in jianzhan: print (item) # Output the content of the website building course # Call the kecheng function 3 times and specify different actual parameters respectively kecheng('html','CSS','JavaScript') kecheng('php','MySQL') kecheng('Thinkphp')
Run the above code as follows:
The website building courses to learn are: HTML CSS JavaScript The website building courses to learn are: PHP MySQL The website building courses to learn are: ThinkPHP >>>
If used, an existing list As a variable parameter of the function, you can add "*" before the name of the list. For example, the following code:
def kecheng(*jianzhan): # Define the function to output the website building course "Print any incoming parameters" print ("The website building courses to learn are: ") for item in jianzhan: print (item) # Output the content of the website building course jianzhan = ['HTML','CSS','JavaScript'] # Define a list kecheng(*jianzhan) # Specify the variable parameters of the function through the list
The operation results are as follows:
The website building courses to learn are: HTML CSS JavaScript >>>
3. Variable parameter with two asterisks * *
Two asterisks: add two * (asterisks) before the formal parameters. Note that there are two asterisks. The functions of the two asterisks formal parameters will Keyword parameters Value as Dictionaries The keyword parameter will be used as a dictionary to traverse inside the function.
For example, define a function so that it can receive any number of actual parameters with explicit assignment. The code is as follows:
def chengji(**chengji): # Define functions that output courses and grades print () # Output a blank line for key ,value in chengji.items(): # Traversal dictionary print(key,"Our achievements are:",value) # Output combined information
Call chengji() function twice. The code is as follows:
def chengji(**chengji): # Define functions that output courses and grades print () # Output a blank line for key ,value in chengji.items(): # Traversal dictionary print(key,"Our achievements are:",value) # Output combined information chengji(language=92,mathematics=98,English=90) chengji(history=88,Geography=90)
The operation results are as follows:
The score of Chinese is: 92 The math score is: 98 The score of English is: 90 The historical achievement is: 88 The result of geography is: 90 >>>
Similarly, if you want to use an existing dictionary as a variable parameter of a function, you can add "* *" before the name of the dictionary. For example, the following code:
def chengji(**chengji): # Define functions that output courses and grades print () # Output a blank line for key ,value in chengji.items(): # Traversal dictionary print(key,"Our achievements are:",value) # Output combined information dict1 = {'language':92,'mathematics':98,'English':90} # Define a dictionary chengji(**dict1) # Specify the variable parameters of the function through the dictionary
After the above code calls chengji() function, the running results are as follows:
The score of Chinese is: 92 The math score is: 98 The score of English is: 90 >>>
only this and nothing more, Webmaster Online The three knowledge points of variable parameters in Python functions explained for you: the definition of variable parameters, variable parameters with one asterisk *, and variable parameters with two asterisks * *. If you have any questions, you can leave me a message!
Pay attention to the webmaster online and learn Python without getting lost! You are welcome to pay attention to, comment on and like the webmaster online, share , forward!