One, Custom function
The function body is not executed when the function is defined, and the function body executes only when the function is called. Structure of the function:
1. def
2. Name of function
3. Function body
def func_name (): function Body
4. Return value
If no return value is declared, the return value defaults to none
def func_name (): Pass return True def func_name (): ' This was to the test how to return ' return mysting
5. Parameters
- Formal parameters
- When defining a function, specify a formal parameter, and several formal parameters are written, eg. def func (ARGS1,ARGS2)
- If there is no formal argument, leave it blank
- Actual parameters
- The arguments passed in when the function is called are called actual parameters, also known as arguments, eg. Func (' Kaye ', 21)
- Default parameters
- When defining a function, specify the default value of the formal parameter, which we call the default parameter, eg. def func (args1, args2 = ' default ')
- The default argument must be placed at the end of the parameter list, and if more than one default parameter is placed at the end of the formal parameter list
- Specify parameter
- specifies that the argument is to be assigned to the corresponding parameter when the function is called to pass in the argument, which is called the specified parameters
- eg. func (args1 = ' Kaye ', ARGS2 = +)
- incoming specified parameter Number, the incoming order of the arguments does not have to follow the order of the parameters when the function is defined, eg func (args2 = 21,ARGS1 = ' Kaye ')
-
- *args
def func (*args) represents a function whose formal parameters are dynamic, where args represents a tuple, an argument is passed in for an instant, and a tuple
- is constructed to receive the serialized data type (dictionary, tuple, string) as its incoming parameter
Call Method: Func (*[' Kaye ', ' Leo ', ' Jack ']) or func (' Kaye ', ' Leo ', ' Jack ')
- at this point args is assigned the value (' Kaye, ' Leo ', ' Jack '), args[0] = ' Kaye ', args[1] = ' Leo ',...
- args is a tuple from the outermost layer, and the elements of a tuple can be lists, dictionaries, tuples, and so on, so that multiple layers of nested serialized data types can be obtained
- **kwargs Kwargs represents a dictionary
- def func (**kwargs), which indicates that the formal parameters of a function are dynamic, where Kwargs represents a dictionary, an argument is passed in for an instant, and a dictionary is constructed
The
- can receive a dictionary as its incoming parameter
- Calling Method: Func (**{' Kaye ': +, ' leo ': +, ' Jack ': 30}) or Func (' Kaye ' =21, ' Leo ' =25, ' Jack ' =30)
- Kwargs is a dictionary from the outermost layer, while a dictionary's key-value pairs can be lists, dictionaries, tuples, and so on, resulting in multiple nested dictionaries
- Universal parameters
- *args,**kwargs
- def func (*args, **kwargs), where args represents a tuple, Kwargs represents a dictionary, an argument is passed in for an instant, and a tuple and a dictionary are constructed separately
- You can receive both serialized data types (dictionaries, tuples, strings), and dictionaries as their incoming parameters
- Calling Method: Func (*[' Kaye ', ' Leo '],**{' Kaye ': +, ' Leo ': 25}) or Func (' Kaye ', ' Leo ', ' Kaye ' =21, ' Leo ' =25)
- This method can be used to construct a multi-layer nested dictionary
6. Supplement:
- function redefined (again later function overloading)
DEF F1
DEF F1
The Python interpreter executes the command from top to bottom, and when the function is redefined, the function is called with a redefinition
- When a function is called into a formal parameter, it is passed a reference to the argument, which means that the operation of the parameter on the calling function is directly acting on the argument, causing the change in the argument
- Global variables
- The global variable is placed at the front of the program, and is equivalent to a constant from the actual action, should all uppercase
- Global variables can be read directly by all functions
- If you want to reassign a global variable in a function, you must precede the variable name with the global
- If the global variable is a dictionary/list, to add or delete the elements in the dictionary list, you do not need the addition of global, write directly according to the usual code
- Comments
- Be sure to write a comment so that someone can see what function is being done
- Generally enter comments after the first line of the function definition, enclosed in three double quotes, "" "" "" "Comment" "
- :p Aram Param1:
- : Return: ...
Two or three-dollar operation (Trinocular operation)
For simple conditional statements, you can use the ternary operation shorthand. Ternary operations can only be written in one line of code
# Writing Format ifelse # Assigns a value of 1 to the result variable if the condition is true, otherwise the value 2 is assigned to the result variable
Examples
' The result if the If succeeds ' if Else ' The result if the if fails and falls to the else part '
Third, lambda expression
For simple functions, there is also an easy way to represent a lambda expression
# ###################### Common function ###################### # defining functions (normal way) def func (ARG): return arg + 1 # execution function result = func (123) # ########## ############ Lambda ###################### # definition function (lambda expression)Lambda arg : Arg + 1 # execution function result = MY_LAMBDA (123)
Iv. built-in functions
Note: View detailed bash here
V. Recursion
Use the function to write the following series:
The Fibonacci sequence refers to a sequence of 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368. ..
def func (ARG1,ARG2): if arg1 = = 0 :print arg1, arg2= arg1 + arg2print
arg3 func (arg2, Arg3) # invokes the current function in the current function definition, embodying the recursive func (0,1)
Python-Functional programming