Nine, the function of python development

1. Three ways to develop
    1. Object-oriented class
    2. Process-oriented def
    3. Functional development def
 
 
Second, the function parameters
1. Set a function and pass parameters with positional parameters (no matter which parameter is passed, the positional parameters must be placed at the top to pass parameters)
def func(x,y): # (Note: x and y are formal parameters.)
    print(x)
    print(y)
func(1,2) #(1 and 2 are actual parameters and positional parameters, pass parameters by position, and give x and y of formal parameters)
 
2. Default parameters of function parameters
def func(x,y,z=1): #(Set the default value at the formal parameter position, and use the default value when the "actual parameter" is not passed in this position when running the function) Note: The default "line parameter" must be written to the back position,
    print(x)
    print(y)
    print (z)
func(2,2) #(two positional parameters are passed, and default parameters are used in the last position)
 
 3. Keyword parameters of function parameters
def func(x,y,z=1):
    print(x)
    print(y)
    print (z)
func(y=2,x=1) #(Use the method corresponding to the keyword to pass parameters)
 
4. Do not set the number of digits to pass parameters
list = ['1','2','3','4'.'5']
dict = employee(name='cai',age=18)
def func(x,y,*args, **kwargs): #Define a *args, which means that there is no limit to the number of parameters passed, or it is used to pass a list
    print(x)
    print(y)
    print(*args)
    print(**kwargs)
func(1,2,3,4,5) #Pass multiple parameters to the function
func(1,2,*list) #Pass the list to the function, the default is an empty tuple if not passed
func(1,2,*list,**dict) #Pass the dictionary as a parameter to the function, if not, it defaults to an empty dictionary
 
3. Higher-order functions
Introduction: 1. Pass a function name as an argument to another function
          2. The return value contains the function name
 
Fourth, recursion:
Introduction: 1. The scale of each recursion should be reduced. The default maximum number of recursion is 999 times.
          2. The efficiency is not high, and the loop call will affect the efficiency of the program.
          3. The end value must be defined. If the end value is not defined, an infinite loop will be formed.
 
Five, function nesting:
Introduction: Define another function inside the function body.
def max(x,y):
    return x if x > y else y
 
def max4(a,b,c,d):
    res1=max(a,b)
    res2=max(res1,c)
    res3=max(res2,d)
    return res3
print(max4(1,2,3,4))

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325324737&siteId=291194637