Write a function
def power (x):
return x * x
For the Power (x) function, parameter x is a positional parameter, and when we call the power (x) function, we must pass in with only one parameter X
Power (5)
Power () is also a built-in function that uses power (X,N) n as an exponent, that is, how many x multiplies
Default parameters
A default parameter can be given when defining a function
def enroll (name,gender,age=6,city= ' Beijing ')
Print (' name: ', name)
Print (' Age: ', age)
When the function enroll is called, the default value of 6 is used if the age value is not passed
Variable parameters
In Python, you can also define mutable parameters
Method one, which tells the passed-in parameter to write a list, as a parameter
Way Two
Def calc (*numbers):
sum = 0
For N in numbers:
Sum=sum + N * N
return sum
When defining a mutable parameter and defining a list or tuple parameter, just precede the parameter with an * number, within the function, the parameter numbers is a tuple, so the function code is completely unchanged, but when the function is called, it can pass in any parameter, including 0 parameters
Calc (=5)
Calc () =0
If the parameter to be passed is a list or tuple, the calling method
nums=[1,2,3]
Calc (nums[0],nums[1],nums[2]) =14
Of course, the above is possible, but it is too cumbersome, so python allows you to precede the list or tuple with an * number, to change the list or tuple elements into a mutable parameter
nums=[1,2,3]
Calc (*nums) =14
*nums means that it is very useful and common to have all the elements of the list nums as mutable parameters.
Keyword parameters
Variable parameters allow you to pass in 0 or any of the parameters, which are automatically assembled as a tuple when the function is called. The keyword parameter allows you to pass in 0 or any parameter that contains the parameter name, which is automatically assembled into a dict inside the function, see example
def person (name,age,**kw)
Print (' name: ', Name, ' Age: ', age, ' other: ', kw)
function person () in addition to the required parameter name and age, also accepts the keyword kw, when the function is called, you can only pass in the required parameters
Person (' Jack ', 30)
You can also pass in any number of keyword arguments
Person (' Jack ', 30,city= ' Beijing ') =name:jack age:30 other: {' City ': ' Beijing '}
Person (' Jack ', 30,city= ' Beijing ', job= ' engineer ') =name:jack age:30 other: {' City ': ' Beijing ', ' job ': ' Engineer '}
Dictionary dict form Incoming
ext={' city ': ' Beijing ', ' job ': ' Engineer '}
Person (' Jack ', 30,**ext) =name:jack age:30 Other: {' City ': ' Beijing ', ' job ': ' Engineer '}
Named keyword parameters,
For key parameters, the caller of the function can pass in any unrestricted keyword argument, and as for which, it needs to be checked inside the function via kw
Still explained in the person () function, we checked for City and job
(age**kw): kw:kw: (NAMEAGEKW)
Person (' Jack ', 24,city= ' Beijing ', addr= ' Chongqing ', zipcode=123456)
Restrict the keyword to receive city and job only, the following wording (special separator *)
(nameage*cityjob): (nameagecity job) person1 (= =)
Non-keyword error
If there is a mutable parameter in the function definition, then the named keyword argument that follows will no longer require a special delimiter *.
(nameage*argscityjob): (nameageargscity Job)
The named keyword argument must pass in the parameter name, which differs from the positional parameter. If the parameter name is not passed in, the call tells the error
The right way
Parameter combinations
In Python definition functions, you can use the required parameters, default parameters, variable parameters, keyword parameters and named keyword parameters, the 5 parameters can be combined, but note that the parameter definition order must be required parameters, default parameters, variable parameters, named keyword parameters and keyword parameters.
(abc=*args**kw): (abcargskw) (abc=*d**kw): (ABCDKW)
But at the time of invocation, the Python interpreter automatically passes the corresponding parameter to the parameter location and argument name
The most magical thing is that the above functions can be called by tuple and Dict.
Therefore, for any parameter, it can be called through the form of the class Func (*ARGS,**KW), regardless of how its arguments are defined
Parameters of the Python function