Python full stack development "basic three" |
The content of this section:
- Functions (global and local variables)
- Recursive
I. Definition and use
function is most important to reduce the reusability of code and enhance the readability of code
1 def function name (parameter): 2 3 ... 4 function Body 5 ... 6 return value
The definition of a function has the following main points:
- Def: A keyword that represents a function
- function name: the names of functions, which are called later by function name
- Function body: A series of logical computations in a function
- Parameters: providing data for the function body
- Return value: Once the function has finished executing, it can return data to the Caller.
Summarize the benefits of using a function:
1. Reduce code Reuse
2. Maintain consistency and ease of maintenance
3. Extensibility
return value
1 defSend mail ():2 3 code to send the message ...4 5 ifSend Success:6 7 returnTrue8 9 Else:Ten one returnFalse a - whileTrue: - the #each time the Send Message function is executed, The return value is automatically assigned to result - - #after that, the log can be written according to result, or the operation will be re-sent . - + -result =Send mail () + a ifresult = =False: at -log, message failed to send ...
second, the parameters
1. Parametric the memory unit is allocated only when called, releasing the allocated memory unit immediately at the end of the Call. therefore, the formal parameter is only valid inside the Function. Function call ends when you return to the keynote function, you can no longer use the shape parametric
2. Arguments can be constants, variables, expressions, functions, and so on, regardless of the type of argument, and when making a function call, they must have a definite value in order to pass these values to the Parameter. It is therefore necessary to use the assignment, input and other methods to get the parameters to determine the value
1 def Calc (x, y) #x, y is the formal parameter 2 res = x**y3 return Res45 c = Calc (2,3) #2,3 is argument 6 Print(c)
3. Positional parameters and keywords (standard call: real participation parameter position one by one corresponds; keyword call: position without Fixing)
Note: the positional parameter must precede the keyword argument
1 defCalc (x, y):2res = x**y3 returnRes4 5c = Calc (y=2,x=3)#keyword Parameters6 Print(c)7 8 9 Ten defCalc (x, y): oneres = x**y a returnRes - -c = Calc (2,y=3)#2 is the positional parameter, y=3 is the keyword parameter the Print(c)
4. Default Parameters
1 def Calc (x,y=3): #y=3 is the default parameter after function does not pass value 2 res = x**y3 return res45# does not pass y value 6Print (c)
5. Parameter Groups
1 ############## #列表 *args2 3 defFunc (*args):4 Printargs5 6 #execution Mode one7Func (11,22,33,55,66)8 9 #mode of execution twoTenLi = [11,22,33,55,66] oneFunc (*Li) a - - ############## #字典 **kwargs the - defFunc (* *kwargs): - PrintKwargs - + - #execution Mode one +Func (name='Ocean', age=18) a at #mode of execution two -Li = {'name':'Ocean', age:18,'Job':'python'} -Func (**li)
third, local variables and global variables
Global variables can be arbitrarily called in a function, but to be modified they must be declared with global
1 ######### #全局变量与局部变量 ########2NAME ="AA"3 4 defcn ():5 GlobalNAME6NAME ="mm"7 Print(">>>>", Name)8 Print(name)#Output AA9CN ()#>>>mmTen Print(name)#mm one a - ########## - #The focus of this example is that functions run only when called theName ="AA" - #Global variables are generally capitalized to differentiate local variables, where lowercase is used temporarily - defAA (): -Name ="BB" + defBB (): - nonlocal name + #nonlocal used to use outer (non-global) variables in functions or other scopes aName ="cc" at BB () - Print(name) - Print(name) - AA () - Print(name)
inside a function, you can call other Functions. If a function calls the function itself internally , the function is a recursive function.
Recursive Properties :
1. There must be a clear end condition
2. Each time a deeper level of recursion is reached, the problem size should be reduced compared to the previous recursion
3. Recursive efficiency is not high, too many recursive levels will cause stack overflow, wasting memory
1 defCalc (n):2 Print(n)3 ifint (n/2) = =0:4 returnN5 returnCalc (int (N/2))6 7Calc (10)8 9 output:Ten10 one5 a2 -1
Python full stack development "basic three"