Description and application of Python function
function definition:
A function is a set of statements that implement a particular function. Functions to simplify scripts, functions include built-in functions and custom functions
Custom Function Framework:
Use DEF in Python to declare a function, the complete function is the function name, parameter, function body, return value.
The declaration function is in the following form:
def function name (parameter) function body return value
Note: parameters and return values are not required, and the return value of the function is null by default when no return value is set.
Function call
In Python, the custom function is the same as the built-in function call method, as long as the function name is used to make the function to be called, and then the function is added with the corresponding parameters, if there are more than one parameter, the different parameters should be separated by ",". A function call must be declared before a function can be called.
Parameter classification in a function
Default Parameters
In Python, you can set a default value for a parameter in advance when declaring a function. When a parameter is called, if a parameter has a default value, you can not pass the argument to the function
def fun (x=5) return X**3print (Fun (2)) 8print (Fun ()) 125
Specifying parameters
If a function has more than one parameter, and these parameters all have default values, the first parameter may be passed when the function is called.
def fun (x=1,y=2,z=3): Return (x+y-z) **3print (Fun (2)) 1print (Fun (3)) 8print (Fun (3,3)) 27
Dynamic parameters
* Default input parameters, all placed in tuples
def fun (*arg:) print (args) fun (11,22) (11,22)
* * The default input parameters, all placed in the dictionary
def fun (**args): print (args) fun (k1=11,k2=22) {k1:11,k2:22}
When there is a * and two * at the same time, define the default parameters when a * must be in front
def fun (*args,**qwargs): print (args) print (Qwargs) Fun (22,33,k1=22,k2=33) (22,33) {' K2 ': +, ' K1 ': 22}
Formatted output%s and%d in Python, also in function format session output format ()
S= "I am {0},age{1}". Format ("Alex") s= "I am {0},age{1}". Format (*["Alex", +]) s= "I am {name},age{age}". Format (name= " Alex ", age=18) s=" I am {0},age{1} ". Format (**dic) s=" I am alex,age "#输出结果
Scope of the variable
In a Python script, different functions can have the same parameter names, the variable names have been declared in the function, and can continue to be used outside the function, and the values do not affect each other while the script is running.
In Python, scopes can be divided into built-in scopes and global scopes and local scopes, with the built-in scope being pre-defined by Python, the global scope being the entire script being written, and the local scope being the inner scope of a function
If the function uses variables outside of the function, you can use the Global keyword before the variable name
def fun (x): #声明一个函数 a=[1] #定义一个名为a的列表 a.append (x) print (a) a=[2,3,4] #在函数外定义一个名为a的列表fun (2) [1,2]a #输出a的 Value
def fun (x): #声明一个函数 Global A return A+xa=5fun (3) 8a=2fun (3) 5
Default-defined global variables all uppercase, local variables all lowercase
Ternary calculation:
If 1==1:
Name= "John"
Else
Name= "hehe"
This article is from the "high-bright" blog, please be sure to keep this source http://gaohl.blog.51cto.com/11862544/1833638
Description and application of Python function