First, Brief introduction
Three kinds of programming: object-oriented class-oriented process def functional programming def
Procedure is a function with no return value
definitionfunction refers to the collection of a set of statements through a name (function name) encapsulated, in order to execute this function, just call their function name to feature: 1, reduce duplicate code 2, make the program extensible (change One, change) 3, make the program easier to maintain
Syntax DefinitionsDef sayhi (): #函数名 print ("Hello, I ' m nobody!") Sayhi () #调用函数可以带参数 Two, parameters of the function 1 def call (m): print (m) Call ( TOM) def Stu_register (name,age,country,course): Print ("----Enroll Student Information------") in this code when m is the formal parameter of Tom as the Argument 2 write (" Name: ", name" Print ("Age:", age) Print ("Nationality:", country) print ("Course:", course) Stu_register ("King Shanbao", "CN", "Python_devops") s Tu_register ("Zhang Jiaochun", +, "cn", "Linux") Stu_register ("Liu Lao Gen", +, "cn", "Linux")
Default Parameters Defining variables in advance find country This parameter is basically "CN", just like we registered users on the site, such as the nationality of this information, you do not fill in, the default will be China, which is implemented by default parameters, the country into the default parameters very simple Def stu_register ( Name,age,course,country= "CN"): 3
Key ParametersUnder normal circumstances, to the function parameters to order, do not want to order the key parameters can be used, just specify the parameter name, but remember a requirement is that the key parameters must be placed after the position parameter 4
non-fixed parametersIf your function is defined without determining how many parameters the user wants to pass, the non-fixed parameter def stu_register (Name,age,*args) can be used: # *args (args is the variable name) will change many incoming parameters into a tuple form print (Name,age,args) stu_register ("Alex") #args是元组形式 # Output #alex () #后面这个 () is args, Just because there is no value, so the empty stu_register ("Jack", "+", "cn", "Python") #输出 # jack (' cn ', ' Python ') can also have a **kwargsdef Stu_register (Name,age,*args,**kwargs): # *kwargs will turn multiple incoming parameters into a dict form print (Name,age, Args,kwargs) stu_register ("Alex", "#kwargs") is the dictionary form # output #alex () {} #后面这个 {} is Kwargs, just because no value is passed, so it is empty stu_ Register ("Jack", +, "cn", "Python", sex= "Male", province= "Shandong") #输出 # jack (' cn ', ' Python ') {' Province ': ' Shandong ', ' sex ': ' Male ' to convert n keyword arguments to dictionaries functions in functional programming functional programming the term does not refer to a function in a computer (actually subroutine), but rather to a function in mathematics, the mapping of an argument. This means that the value of a function is determined only by the value of the function parameter, not by other states. For example, the sqrt (x) function calculates the square root of X, as long as x is not changed, and whenever the call is called, the value is constant. Functional programming Input OK, output OK function is not the same inside there is logic to determine if X>7:return 0 input determines the output uncertainty Four, the higher-order function variable can point to the function, the function parameter can receive the variable, then one function can receive another function as a parameter, This function is called the higher order function. Def add (x,y,f): return f (x) + f (y) res = add (3,-6,abs) print (RES) ABS () take absolute value
Python Learning Path (Basic)--function