In this article we will look at the parameters in Python, the parameters in Python is a more important and commonly used knowledge points. Example
python3 keyword ParametersLater use also more, in Baidu on a search
python keywordIt will pop out. So in this article, let's study.
python keyword Parametersand python required parameters.
First we need to know the type of the parameter, as follows:
Type of parameter:
Required Parameters
Keyword parameters
Default parameters
Indefinite length parameter
Essential functions
The required parameters must be passed into the function in the correct order. The number of calls must be the same as when declared. Call the Printme () function, you must pass in a parameter, or a syntax error will occur:
#!/usr/bin/python#-*-coding:utf-8-*-#可写函数说明def printme (str): "Print any incoming string" Print str; Return #调用printme函数printme ();
The result of the above example output:
Traceback (most recent): File "test.py", line one, in <module> printme (); Typeerror:printme () takes exactly 1 argument (0 given)
Let's take a look at the keyword parameter
Keyword parameters:
Keyword arguments are closely related to function calls, and function calls use keyword parameters to determine the values of the parameters passed in.
Using the keyword argument allows a function call when the order of the arguments is inconsistent with the Declaration, because the Python interpreter can match the parameter values with the name of the argument.
The following instance uses the parameter name when the function Printme () is called:
#!/usr/bin/python#-*-coding:utf-8-*-#可写函数说明def printme (str): "Print any incoming string" Print str; Return #调用printme函数printme (str = "My string");
The result of the above example output:
My string
The following example shows that the keyword parameter order is not important to show more clearly:
#!/usr/bin/python#-*-coding:utf-8-*-#可写函数说明def printinfo (name, age): "Print any incoming string" Print "name:", name;
print "age", age; Return #调用printinfo函数printinfo (age=50, name= "Miki");
The result of the above example output:
Name: mikiage 50
The above is what we want to explain today, the classification of parameters and the parameters of the necessary parameters and keywords. Although the above knowledge point to give an example, but still need to try it yourself, after all, they do not do the same as on paper, hands-on practice is the best way to verify the learning. Finally, I hope that this article will give you a little help in learning about Python.
For more information, please visit the PHP Chinese Web Python tutorial section.