A. function
A function is a programmatic way to logically structure and process a set of statements that are encapsulated by a name (function name), and to execute this function, simply call the name of its functions
(1) The advantage of using a function is the reusability of the code, that is, by calling the function to reduce the duplication of code. Keep your code consistent. Make your code more extensible
(2) mainly divided into: (Process-oriented and object-oriented differences are: Object-oriented is a return value, and process-oriented is not a function of return value)
1. Process oriented
def func (): Print ("in thefunc") ×= Fun()print("from Funreturn Is%s"%x" >>>>>>>none# When the function does not define a return value Yes, the system returns none by default
View Code
2. Object-oriented
def func ():# defines the function print("in thefunc") returnx=func ()# invokes the function and assigns the value xprint("from Func return ID%s"%x"# output function return value >>>>>>>0
View Code
3. Functional programming
(3) Return value considerations:
1. When the function returns a value of return 0 o'clock, return 0
2. Returns none when the function does not define a return value
3. When the function return value is greater than 0 o'clock, returns the data of a primitive type, that is, return "a" returns ("a")
Two. Parameters
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
arguments can be constants, variables, expressions, functions, and so on, regardless of the type of argument, and when a function call is made, 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
def Fun (x, y):# The X and y here are formal parameter print("x=%s,y=%y"% (x, y)) fun (3,4)# 3 and 4 Here are arguments >>>>>>x=3,y=4
Default parameters and key parameters
The default parameter is the key parameter that is defined when the function is defined that the calling function is defineddefFun (a,b,c=" +"):#The c here is defined by the definition of the function, which indicates that the default parameter of C Print("a=", a)Print("b=", B)Print("c=", C) Situation one: Fun (10,20,30)#the C parameter passed in here overrides the default value. If no arguments to C are passed, the default parameters are outputScenario Two: Fun (10,A=10,)#under normal circumstances, to the function of parameters to order, do not want to order the key parameters can be used, just specify the parameter name canView Code
What is a non-fixed parameter: If your function is defined without determining how many parameters the user wants to pass in, you can use the invariant parameter
defStu_register (Name,age,*args):#*args will change multiple incoming parameters into a tuple form. Print(Name,age,args) stu_register ("Alex", 22)#Output#Alex () #后面这个 () is args, just because no value is passed, so it's empty.Stu_register ("Jack", 32,"CN","Python")#Output#Jack (' CN ', ' Python ')
defStu_register (Name,age,*args,**kwargs):#*kwargs will turn multiple incoming parameters into a dict form. Print(Name,age,args,kwargs) stu_register ("Alex", 22)#Output#Alex () {} #后面这个 {} is Kwargs, just because no value is passed, so it is emptyStu_register ("Jack", 32,"CN","Python", sex="Male", province="Shandong")#Output#Jack (' CN ', ' Python ') {' Province ': ' Shandong ', ' sex ': ' Male '}
Python: A preliminary understanding of functions