Python functions in a detailed
This content includes:
- Function
- Module
- File operations
- Exception handling
Function:
- Cognitive functions
- Local variables and global variables
- Explanation of function parameter usage
- function Use Combat
The essence of function is the function of the sub-assembly. For example, we encapsulate a function c=a*a that asks for a 2-time square. We can use this function if we ask for a 2-time change (just for example).
The POW (x, y) in Python is the function that asks for X's Y-square.
Function: Provides the programming efficiency and the scale of the program
Local variables and global variables
A variable has an effective scope, which is scoped.
Global variables: Scope from the beginning of a variable to the last variable on the program to make a global variable
Local variables: Scope only local variables are called local variables "
" " Scope " " a=10def ABC (): Global J#用global定义局部变量 changed to globally variable J=1 Print (a) ABC ()print(j)
How to use the function:
Function call
# The function definition calls a=1def ABC (): #def定义函数 followed by the function name and: print(" ABC") #具体方法, let the function do what, for example: Print out ABC call method: ABC ()
Output ABC ()
Use of function parameters
Argument: The parameter used for the definition is an argument, and the parameter used when invoked is called an argument
Formal parameters: Just the form of this parameter, no specific value, no value is a formal parameter.
Invoked as an argument.
# formal parameters and arguments def AB (A, B):# The parameters are separated by commas, AB is a formal parameter, there is no actual value if a>B: Print (a) elif a==B: Print(A +"= "+B) Else: print(b)
Call AB (+)
Output is 2
Python Basics 03-Functions in a detailed