Functions are primarily intended for code reuse.
There are two types of functions: System library pre-defined functions, custom functions.
function format:
def functionname ():
Statement
Function call:
Funtionname ()
Parameters of the function: formal parameters and arguments
Formal parameters: Parameters in the list of parameters at the time of defining the function, referred to as the formal parameter.
Actual parameters: Parameters in the parameter list at the time of calling the function, referred to as arguments.
The argument and the formal parameter are one by one corresponding.
Default parameter: When defining a function, assign the initial value to the parameter.
The default parameter is to be aware of the parameter order. The default parameters are generally in the following sections.
Variables of the function
Global variables and local variables
Local variables: variables defined in the body of the function. Can only be used within the function body, not allowed outside the function body.
Global variables: Variables defined outside the body of a function. Can be used in the body of a function or outside the body of a function.
If the global variable and local variable have the same name:
Inside the function is a local variable within the function body.
If used outside the body of the function, the global variable is used.
Define a global variable in the function body:
Global y
y=200
function return value:
The function must have a return value. There are three types of cases:
1.return A
2.return; return None by default
3 default condition. Returns a none by default.
Redundant parameters of the function:
Multi-type pass-through and value-transfer redundancy.
Multi-type pass-through values:
Transitive tuple: f (*t)
Transitive dictionary: F (**t)
The parameters of the key and function are to be kept in the same name.
Value-Transfer redundancy:
To pass extra arguments to a function:
def function (X,*args):
Print X
Print args
---pass-through tuples
def function (y= "a", **args):
Print Y
Print args
---pass-through dictionary
def f (X,*args, **kwargs):
Print X
Print args
Print Kwargs
---pass tuples and dictionaries
Lambda functions:
anonymous function: The smallest function that defines a single line quickly.
def f (x, Y):
Return X*y
The corresponding lambda expression:
G=lambda:x,y:x*y
G (2,3)
The basis of Lambda:
The semicolon is preceded by a parameter, and multiple parameters are separated by commas. The return value to the right of the colon.
Switch statement:
There is no switch keyword in python. It needs to be implemented using a dictionary.
Built-in functions:
Absolute value abs, Min min, max Max
Len (), Divmod (), POW (), round ()
Callable (), Isinstance (), CMP (), Rang (), Xrang ()
Type (), int (), long (), list (), complex () ...
String built-in function: (The following STR represents the Str object)
Str.capitalize (): Capitalize first letter
Str.replace (Sourcestr, Deststr[,count]): Replace SOURSTR with DESTSTR
Str.split (delimiter, maximum number of cuts N): Divides str with a delimiter, up to n chunks.
After you import the string module, you can use String.Replace (s,old,new,n)
Filter (function,sequence), zip (), map (), reduce (): the first parameter of the Filter,map,reduce function is the functions.
Note that you distinguish between zip and map, which differs from the processing result of a list of different lengths.
Zip follows the minimum processing. The map will fill the void with none.
Python Learning Note 2---function