First, the definition of the function format:
def function name (parameter list):
function body
Def fun1 (A, B, c): return a + B + C
Second, position transfer: Position correspondence
Print (FUN1 (3, 2, 1))
Output:
6
Keyword delivery: positional parameters before the keyword parameter
Print (FUN1 (3, c = 1, b = 2))
Output:
6
Parameter default value: You can assign a default value to a parameter
Def fun2 (A, b, C = +): return a + B + cprint (fun2 (1)) Print (FUN2 (1, 10, 0))
Output:
111
One
V. Parcel location Transfer: In the parameter table, all parameters are collected, merged into tuples according to location
When defining a function, you need to add * to the parameter
def fun3 (*tup): print type (tup) print tupfun3 (1) fun3 (1, 2) fun3 (1, 2, 3)
Output:
<type ' tuple ' >
(1,)
<type ' tuple ' >
(1, 2)
<type ' tuple ' >
(1, 2, 3)
Six, parcel keyword delivery: In the parameter table, all parameters are collected, according to the keyword merged into a dictionary
To define a function, you need to add the parameter before the argument
def fun4 (**dic): print type (DIC) print dic fun4 (a=1,b=9) fun4 (m=2,n=1,c=11)
Output:
<type ' dict ' >
{' A ': 1, ' B ': 9}
<type ' Dict ' >
{' C ': One, ' m ': 2, ' n ': 1}
Seven, unpacking
* Each element corresponds to a positional parameter
* * Each key value pair as a keyword
Def fun5 (A, B, c): print (A, B, c) args = (2, 1, 3) fun5 (*args) dict = {' B ': 1, ' a ': 2, ' C ': 3}fun5 (**dict)
Output:
(2, 1, 3)
(2, 1, 3)
Eight, mixed
When defining or invoking parameters, several methods of passing parameters can be mixed. But be careful about the sequence in the process.
Basic principles: location > Keywords > Parcel location > Parcel keywords, and distinguish them according to the principles described above.
Python Learning Note 5: A detailed description of the function parameters