Function basics
First, what is a function
Functions are tailored to the needs of the development language to achieve a certain function. You can then call the function directly when you encounter the scenario.
Second, the classification of functions
Functions are divided into two categories:
1, built-in functions: for the convenience of development, for some simple functions, the Python interpreter has defined for us the function is built-in function.
For built-in functions we can use them without implementing a definition. such as: Len () sum () join (), etc.
2. Custom functions: The functions provided by the built-in functions are limited, so we need to define the functions in advance according to our own requirements, then we can call them directly when we encounter other application scenarios.
Third, how to customize the function
# Grammar def function name (parameter 1, parameter 2, Parameter 3 ...) " " function Body return value # function name It's best to reflect its meaning
def Test (x): " " 2*x+1 :p Aram x: Shaping number : return: Returns the calculated result ' y=2*x+1 return y
Iv. return value of a function
When should I have a return value? Call the function, after a series of operations, finally to get a definite result, you must have a return value usually has the parameter function needs to have the return value, the input parameter, after computation, obtains the final result when does not need to have the return value? Call a function, just perform a series of operations, and finally do not need to get any results, there is no need to have a return value normally no parameter function needs to have a return value
No return->None return 1 values-- returns 1 values Comma separated multiple values--tuples
V. Parameters of the function
Parameters are divided into formal and actual arguments:
Formal parameter: The parameter is actually the variable name, the function calls the value to help the variable name, the function call ends, unbind.
Arguments: arguments are variable values
Specific application:
Position parameters
Positional parameters: parameter positional parameters defined in left-to-right order : Required parameter positional argument: Pass value to parameter by position
add: Position parameters must be one by one corresponding, indispensable. def Calc (x, y ):= x**yreturn= Calc (2,3) # Position Parameters
Keyword parameters
Keyword parameter: An argument that is defined in theform of key= value does not need to be noted by location asa parameter: 1. The keyword argument must be on the right side of the positional argument 2. Cannot repeat values for the same parameter def Test ( x , Y, z): # x=1,y=2,z=3 Print print(y) print(z)# keyword parameter, no one by one corresponding, Not one more , no more. The keyword argument must be to the right of the location argument
Default parameters
default parameters: When the parameter is defined, it is assigned a value that can be passed or not, and often needs to be defined as positional parameters, and the smaller parameter is defined as the default parameter (formal parameter) Note:1. Assign a value only once at the time of definition2the definition of the default parameter should be to the right of the location parameter3default parameters should usually be defined as immutable typesdefHandle (x,type='MySQL'): Print(x)Print(type) handle ('Hello')#assign a value to xHandle'Hello', type='SQLite')#' Hello ' assigns a value of X,type to ' SQLite '
Variable length parameter: * List * * Dictionary
Variable length parameter: variable length refers to the number of arguments is not fixed and the actual parameters are defined by position and by the keyword, for the two forms of variable length, formal parameters corresponding to the two solutions to the complete storage of them, respectively, *args,** Kwargs def# list of print(x, y )print (args) foo (1,2,3,4,5) def foo (x,y,**kwargs): # The dictionary holds print(x, y)print(kwargs) foo (1,y=2,a=1 , b=2,c=3)
Python_ Function Basics