function is to encapsulate a piece of common code in the program, a name, the other parts of the program can be easily called
Definition of a function
def function name (parameter 1, parameter 2 ...)
Do something ...
return value/object, function, etc.
# CODING:GBK def Python_class (n): Print (" Saturday has%s bit to learn python" % n ) return Truepython_class (20)
Parameter type
1: Normal
2: Default parameter
3: Variable length parameter
# CODING:GBK def Test_args (a,b=1): print( b)print(A +b) test _args (3) Test_args (3,2)
Variable length parameter : when calling a function, you can pass in any number of arguments in any form.
* On behalf of Ganso, all parameters with no key specified will be placed in a tuple.
* * means that the dictionary will put all the parameters of the specified key into the dictionary.
*args:tuple
**kwargs: That's when you pass in Key=value is a stored dictionary
# CODING:GBK def Test_args (*args,**Kwargs): print(Args,kwargs) Test_args (3,5,a=1 , b=2)
return value of the function
# CODING:GBK def Test (m,n): return M,n Print (Test (1,2A))
Python function basics