function ******************650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M02/9C/1F/ Wkiol1lscc3ranq_aad0d2l6sce889.png "title=" 234520170717154716.png "style=" Float:none; "alt=" wkiol1lscc3ranq_ Aad0d2l6sce889.png "/>
-
Defining functions
def keyword, write down the function name, parentheses, arguments in parentheses, and colons:
The function body is written in the indent block, and the return value of the function is returned with a return statement.
def func ():
Pass--pass Placeholder
Return--Returns statement
#函数在执行过程中一旦遇到return, the function is executed and the result is returned.
#函数在执行过程中没有遇到return时, the return value is none.
[Object Object]
650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M02/9C/1F/wKiom1lscc6gRn-1AADERvsl4vA506.png "title=" 234520170717161038.png "style=" Float:none; "alt=" Wkiom1lscc6grn-1aadervsl4va506.png "/>
650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/9C/1F/wKioL1lscc_AaWaFAACzJr6Iy7c454.png "title=" 234520170717161053.png "style=" Float:none; "alt=" Wkiol1lscc_aawafaaczjr6iy7c454.png "/>
-
Call function
Python has a lot of useful functions built into it, and we can call it directly:
ABS () --the absolute value function
CMP () --Comparison function
Data type conversion function:
int (), float (), str (), Unicode (), BOOL () 650) this.width=650; "Src=" https://s2.51cto.com/ Wyfs02/m00/9c/1f/wkiol1lscc2x8a5waabyrwxhiqk481.png "title=" 234520170717161000.png "style=" Float:none; "alt=" Wkiol1lscc2x8a5waabyrwxhiqk481.png "/>
650) this.width=650; src= https://s1.51cto.com/wyfs02/M01/9C/1F/ Wkiol1lscc6y9vieaabkqrd6ey8210.png "title=" 234520170717161027.png "style=" Float:none; "alt=" Wkiol1lscc6y9vieaabkqrd6ey8210.png "/>
-
Understanding function Names defining functions
def keyword, write it in sequence
A function name is similar to a variable name, which is actually a reference to a function object;
An "alias" is given to the function: The function name is assigned to a variable
650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/9C/20/wKiom1lsdVzyDstuAAA0SlmXAhY756.png "title=" 234520170717162801.png "alt=" Wkiom1lsdvzydstuaaa0slmxahy756.png "/>
-
Null function
Define an empty function that does nothing and can use pass statements;
Pass can be used as a placeholder, not yet how to write the code of the function, you can put a pass, so that the code can run.
Def nofunc ():
Pass
650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M00/9C/1F/wKiom1lscc_AkZyWAACjlGpqm6M977.png "title=" 234520170717161105.png "style=" Float:none; "alt=" Wkiom1lscc_akzywaacjlgpqm6m977.png "/>
- The
-
Parameter checks
when calling a function, if the number of arguments is incorrect, the Python interpreter automatically checks out and throws TypeError;
If the parameter type is not correct, the Python interpreter will not be able to check it for us.
data type checking can be implemented with built-in function isinstance
650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M00/9C/1F/wKiom1lscdCgnVKZAAC3rzbz1Yw033.png "style=" Float:none; "title=" 234520170717161116.png "alt=" Wkiom1lscdcgnvkzaac3rzbz1yw033.png "/>
650) this.width= 650, "src=" Https://s4.51cto.com/wyfs02/M00/9C/1F/wKioL1lscdChcYtaAACzK-XN_IY004.png "style=" Float:none; "title=" 234520170717161125.png "alt=" Wkiol1lscdchcytaaaczk-xn_iy004.png "/>
- The
-
Function return value
function can return more than one value? Then write the Python program and think about it:
defines a function func, passing in two digits, returning the average and maximum values of two digits. The
function returns the value with the return keyword;
Returns a tuple to omit parentheses;
Python's function returns a multivalued value, which is the return of a tuple function without a return statement, automatically return none
650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M00/9C/1F/wKioL1lscdGBXlh6AAEZQuwD6ls791.png "style=" Float:none; "title=" 234520170717161135.png "alt=" Wkiol1lscdgbxlh6aaezquwd6ls791.png "/>
650) this.width= 650, "src=" Https://s5.51cto.com/wyfs02/M01/9C/1F/wKiom1lscdKjngKFAAEOqx0HhR4285.png "title=" 234520170717161142. PNG "style=" Float:none; "alt=" Wkiom1lscdkjngkfaaeoqx0hhr4285.png "/>
- The
-
Function parameter
Default parameter can reduce the difficulty of calling a function. The
defines a function that calculates the X-value of the n-th square. What if you only need to pass in the X value when you calculate x squared?
★ Default Parameter considerations:
When there are multiple parameters, the changes are placed in front of the change, the small changes in the back;
The required parameter is before the default argument in the
650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M01/9C/20/ Wkiol1lsdnywjdt5aadywrdhnte176.png "style=" Float:none; "title=" 234520170717163238.png "alt=" Wkiol1lsdnywjdt5aadywrdhnte176.png "/>
650) this.width=650; src= https://s2.51cto.com/wyfs02/M01/9C/20/ Wkiom1lsdn3w05t0aadywwwbjea234.png "style=" Float:none; "title=" 234520170717163247.png "alt=" Wkiom1lsdn3w05t0aadywwwbjea234.png "/>
-
Default parameters
# default parameters, When a required parameter exists in the function, be sure to place the required parameter before the default parameter;
# Set the default parameters, the change of the large parameters in front, small changes in the parameters, small changes can be set to the default parameters.
650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M01/9C/1F/ Wkiol1lscdkhui-raaejfxaudig478.png "style=" Float:none; "title=" 234520170717161152.png "alt=" Wkiol1lscdkhui-raaejfxaudig478.png "/>
650) this.width=650; src= Https://s4.51cto.com/wyfs02/M01/9C /1f/wkiom1lscdpslwxraaevkoidgnq259.png "style=" Float:none; "title=" 234520170717161200.png "alt=" Wkiom1lscdpslwxraaevkoidgnq259.png "/>
650) this.width=650; src= https://s1.51cto.com/wyfs02/M02/9C/1F/ Wkiom1lscdtwwvntaad3hnxchdg098.png "style=" Float:none; "title=" 234520170717161210.png "alt=" Wkiom1lscdtwwvntaad3hnxchdg098.png "/>
-
Variable parameter *args
-variable parameter is the number of parameters passed in is variable, can be one, 2 to any, or 0.
# when defining a function, the parameter can be *args, so that the function can receive more than one parameter, and the received data type is a tuple;
# If you want to pass a list or tuple into a function, you can pass the argument to the function via *li or *t.
-
Keyword parameter **kwargs
The keyword parameter allows you to pass in 0 or any parameter with parameter names;
These keyword parameters are automatically assembled into a dict within the function;
Keyword parameters with **kwargs
-
Parameter combinations
Parameter combinations are used with required parameters, default parameters, variable parameters, and keyword parameters.
The order of the parameter definitions must be: required, default, variable, and keyword parameters.
-
Summary of parameters
Required parameters > Default parameters > Variable parameters (*args) tuple > keyword parameter (**kwargs) dictionary
1). Required Parameters
2). Default parameters: Default parameters must be used with immutable objects
3). Variable parameter: *args is a mutable parameter, and args receives a tuple of tuples
4). Keyword parameter: **kwargs is the keyword parameter, kw args receives a dictionary dict
For any function, it can be called via a Func-like (*args, **kwargs) Form.
-
Scope of the variable
Local variables: Variables that can only be used inside a function
Global variables: Variables used throughout the program
Global keyword: Forcing local variables to be converted to global variables
650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M01/9C/1F/wKioL1lscdTQlKwWAACblvRTQvI094.png "style=" float : none; "title=" 234520170717161217.png "alt=" Wkiol1lscdtqlkwwaacblvrtqvi094.png "/>
650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M00/9C/20/wKioL1lsdsijJSr9AACtdxZV4_g290.png "style=" float : none; "title=" 234520170717161236.png "alt=" Wkiol1lsdsijjsr9aactdxzv4_g290.png "/>