Learning summary on September 28
1, Function basis
1. Cognitive function
a. Concept (machine): a function is the encapsulation of code that implements a specific function
b. Classification of functions
1. System functions (machines built by the system) - functions created by Python, such as print,input,type,max,min,id, etc
2. User defined function (self built machine) - a function created by the programmer
2. Define function (create function)
1. Syntax:
def function name (formal parameter list):
Function description document
Function body
2. Description:
a. def - keyword; Fixed writing
b. Function name - named by the programmer
Requirement: identifier; Cannot be a keyword
Specification: see the name to know the meaning (see the function name to know what the function is);
All letters are lowercase, and multiple words are separated by underscores;
System function names, class names, and modules cannot be used.
c. () - Fixed writing
d. Formal parameter list - exists in the format of 'variable name 1, variable name 2, variable name 3,...'.
Each variable here is a formal parameter. If you don't need a formal parameter, you don't need to write anything in the parentheses
Formal parameters are used to transfer data outside the function to the inside of the function (can be understood as linking channels outside and inside the function, only in and out)
Whether formal parameters are needed depends on whether additional data is needed to realize the function of the function
e. : - Fixed writing
f. Function description document - essentially document comments
g. Function body - one or more statements that maintain an indentation with def;
The function body is the code that implements the function of the function
# Exercise 1: define a function to sum any two numbers def sum2(num1, num2): print(num1 + num2) # Exercise 2: define a function and find the factorial of N def factorial(n): result = 1 for x in range(1, n+1): result *= x print(result)
2, Call function
1. Call function (using machine)
1. Important conclusions
When defining a function, the function body will not be executed, and the function body will be executed only when calling data (important!)
2. Call function
Syntax: function name (argument list)
explain:
a. Function name - the name of the function to be used. (the function name must be a defined function name)
b. () - Fixed writing
c. Argument list - multiple data are separated by commas: data 1, data 2, data 3
(in principle, when defining a function, you need as many arguments as you need to call the function.)
# 1) The calling function executes the function body (as many times as it is called) def fun1(): print('=======') print('+++++++') fun1() fun1() # 2) In principle, when defining a function, you need as many arguments as you need to call the function def fun2(): pass fun2() # fun2(10) # TypeError: fun2() takes 0 positional arguments but 1 was given def fun2(x): pass # fun2() # TypeError: fun2() missing 1 required positional argument: 'x' # fun2(10 ,20) # TypeError: fun2() takes 1 positional argument but 2 were given # 3) Each argument can be any resulting expression def fun4(x): pass fun4(10) a = 10 fun4(a) fun4(200 - 100 * 2 + 3) fun4('abc')
3, Return value: pass the data inside the function to the outside of the function
1. Take a data as the return value of the function
Syntax: return Data to be returned (data to be passed from inside the function to outside)
2. How to get the return value of a function outside the function
To get the value of the function call expression is to get the return value of the function. (the value of the function call expression is the return value of the function)
Function call expressions can do whatever the return value can do.
def sum2(num1, num2): return num1 + num2 x = sum2(10, 20) print(x) # Exercise: what is the print result of the console after executing the following code? def func(x, y): x.append(y*2) return x list1 = [func([10, 20], 30), 'abc'] print(list1[0][-1]) # 60
4, return keyword
1. Function of return keyword
1. Determine the return value of the function (transfer the data inside the function to the outside of the function)
2. End the function in advance (if return is encountered when executing the function body, the function will end directly)
def func1(): print('=====') return print('+++++') print('------') result = func1() print(result) # ===== # None
5, Parameters of function
1. Location parameter and keyword parameter - the category according to the method provided by the argument
1. Location parameter - directly separate multiple data with commas, and make the arguments and formal parameters correspond one by one from the location (the first argument assigns a value to the first formal parameter, and the second argument assigns a value to the second formal parameter...)
2. Keyword parameter - parameter is passed in the form of 'formal parameter = actual parameter'
3. Mixed use of location parameters and keyword parameters - location parameters must precede keyword parameters
def func1(x, y, z): print(f'x:{x}, y:{y}, z:{z}') # Position parameters func1(10, 20, 30) # Keyword parameters func1(x=10, y=20, z=30) func1(y=20, z=30, x=10) # Mixed use func1(10, 20, z=30) func1(10, z=30, y=20)
2. Parameter default value
1. When defining a function, you can assign default values to parameters through formal parameter = data. Parameters with default values can be called without parameters.
2. When defining a function, you can only assign default values to some parameters. At this time, parameters without default values must precede those with default values.
def func2(x=10, y=20, z=30): print(f'x:{x}, y:{y}, z:{z}') func2() func2(100) func2(100, 200) func2(100, 200, 300) func2(y=200)
3. Parameter type description and return value type description
1. Parameter type description:
a. No default parameter: type name
b. Assign default value to parameter
2. Return value type description: - > type name
def func4(x: list, y='') -> list: pass
4. Indefinite length parameter - a formal parameter can accept multiple arguments at the same time
Add * * * * * before the parameter name, then this parameter can accept multiple arguments at the same time (it must be a positional parameter).
Principle: the parameter with * will become a tuple, and the received argument will become an element in the tuple
Note: variable length parameters with * can only be used as position parameters when transferring parameters
**Important conclusion: * * when defining a function, if there is an independent * in the parameter, its function is to make the parameter after * pass the parameter with keyword when calling the function
If ****** is added before the parameter name, this parameter can accept multiple arguments at the same time (it must be a keyword parameter).
Principle: parameters with * * will become a dictionary, and the accepted arguments will become key value pairs in the dictionary
(1)*
# Exercise: define a function and sum multiple numbers # sum1(10, 20) sum1(10, 20, 30) sum1(1, 2, 3, 4, 5) def sum1(*x): return sum(x) result = sum1(1, 2, 3, 3, 3, 3, 3, 4) print(result)
def func6(*x, y, z): print(f'x:{x}, y:{y}, z:{z}') # Conclusion: if the fixed length parameter follows the variable length parameter corresponding to *, the keyword parameter must be used when transmitting the fixed length parameter func6(10, 20, 30, y=40, z=50) # Conclusion: when defining a function, if there is an independent * in the parameter, its function is to make the parameter after * pass the parameter with keyword when calling the function def func7(x, *, y): pass
(2)**
def func9(**x): print(x) func9() # {} func9(n=10) # {'n': 10} func9(n=10, a=20, m=30) # {'n': 10, 'a': 20, 'm': 30}
6, Define the basic flow of the function
Step 1: determine function
Step 2: determine the function name according to the function
Step 3: determine the formal parameters (see how many additional data are needed to implement the function)
Step 4: realize the function of the function (use the formal parameter as the corresponding data)
Step 5: determine the return value of the function
Step 6: write function description document