Function
Definition, encapsulates a set of code with a name (function name), and when you want to execute this set of code, call the function name directly
Features, reduce duplication of code, make programs extensible, and make programs easier to maintain.
def hello () #def关键字, followed by the function name print ("Hello python!") #此处使用的是Python3. x syntax
def add (x, y) #x, Y is the function's argument (here is the formal parameter) ' ' ' Comment, which describes the function ' res = x+y return res #返回值 (can have no return value, no return value specified, default is null) A = Add (2+4) #调用函数 and assigns the return value of the function to variable a (where 2 and 4 are arguments) print (a) >>>6
Parameter: The memory unit is allocated only when it is called, and the allocated memory unit is freed 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 amount of the actual argument, they must have a definite value when making a function call 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
The parameters can be 0 or more:
Def show (*args) #将传入的参数变成一个元组形式 print (args) show ("Fly", "up to", "it") >>> ("Fly", "**kwargs", "it") def show (*) # Convert the passed parameter into a dictionary form print (Kwargs) show ("Name" = "Fly", "Age" =23, "dept" = "IT") >>>{"name": "Fly", "Age": "Dept": " IT "}
Nested functions
As the name implies, in the function a body in the definition of a function B. At this point, the scope of function B is only within function A, so you can call function B externally only by calling function A.
Def change_name (): name = "Fly" Def change_name2 (): name = "Fly2" Print ("2nd print", name) change_name 2 () #调用内层函数 print ("1th print", name) Change_name ()
Higher order functions
The parameter of a function is another function, which is the function of the function is the higher order function
Built-in functions
650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M00/9D/90/wKiom1mB3SaQNdb0AAbXej4JDsI983.png "title=" built-in functions. PNG "alt=" Wkiom1mb3saqndb0aabxej4jdsi983.png "/>
This article is from the "Technology Rejuvenating" blog, please make sure to keep this source http://8097612.blog.51cto.com/8087612/1953112
Python Learning Path-functions