One: define
Encapsulation and definition of code blocks and functions
Two: function definition, function name, function body and function call
We use the DEF keyword to define the function, which defines the syntax for the function:
def function name (): function Body
The naming conventions and variables for function names are basically consistent,
The function body is the code to execute after the function is executed.
The function name () is used in the form of a function call.
Three: Return of function
The return keyword breaks the function and returns a return value
About the return value:
If return doesn't write anything or not, simply don't write return. So the return is none.
What if the return post is written? A value. Then the user of the tune can receive it. One result
If a return is written with multiple results, the user can receive it. A tuple, the user can directly solve the formation of multiple variables
Four: Parameters of the function
def function name (parameter): function Body
First, you need to identify two concepts, formal parameters and arguments
Formal parameter: The variable written in the declaration position is called the formal parameter, it represents the complete form, indicating that the function requires XXX
Arguments: Values passed to the function when the function is called, expressed to the function xxx
Parameter: The process of handing an argument to a parameter is called a reference
4.1 Classification of arguments
1. Positional parameters: The arguments are passed to the formal parameters according to their positional order, and the interpreter defaults to the location parameter.
defYue (chat, address, age):#Formal ParametersPrint("get the phone .")Print("Open"+chat)Print("Find a"+address+"near the beautiful"+STR (age) +"Old sister")Print("about not about") Yue ("","Beijing", 18)#actual Parametersresults: Take out your cell phone and find a beautiful 18-year-old sister Jobiyo near Beijing.
2. Keyword parameters: Do not follow the location of the parameters, according to the parameter name to pass the argument
defYue (chat, address, age):Print("get the phone .")Print("Open"+chat)Print("Find a"+address+"near the beautiful"+STR (age) +"Old sister")Print("about not about") Yue (chat="", Age=18, address="North Beijing")#keyword parameter.results: Take out your cell phone and find a beautiful 18-year-old sister Jobiyo near Beijing.
3. Mixed parameters: Mix the above two parameters, but the position parameter must be in front of the keyword parameter
# Mixed parameter Yue ("", age=18, address=" on? ) " # correct. The first position is assigned to chat, and the following argument begins with the specified keyword. Yue (age="" ", address=" Guangzhou "# error, the first use of the keyword parameters, then the position behind the string, prone to confusion
4.2 Classification of formal parameters
1. Position parameter: The position parameter of the same argument, it is easy to understand
2. Default parameter: When this parameter does not pass the argument, the default value is used, which is required after the position parameter
# For example, we enter the basic information of a class student. It was found through the survey. Most of the students are boys. At this point, you can give a sex= ' man ' default value. def stu_info (name, age, Sex= " male " print (" Enter student information Span style= "COLOR: #800000" > " ) print (name, age, Sex) print ( " input complete " ) stu_info ( " Zhang Qiang ,)
3. Dynamic Parameters
3.1 Dynamic Accept positional parameter, dynamic parameter must be after position parameter, before default value parameter, * means accept arbitrary content
def Chi (* food): print(" I'm going to eat ", "meal") Chi (" Big Rice " " small rice "result: I want to eat (' big Rice ' Little Rice '# Multiple parameters are passed in. The received content is tuple tuples
3.2 Dynamic receive keyword parameters, using * * flag
def func (* *Kwargs) :print(Kwargs) func (a=1, b=2, c=3) func (a=1, b=2 Result: {'a'b'C' : 3} {'a''b': 2}
Final ordering of several parameters: Position parameters > *args > Default Parameters > **kwargs
If you want to receive all types of parameters, you can use Def func (*args, **kwargs): The form, you can receive any type of parameters, the source of many methods are this form.
4 Another way to dynamically pass a parameter
def Fun (*args): print= [1, 4, 7]fun (lst[0], lst[1], lst[2]) fun ( # You can use * to break a list in order " I can't do it "fun(# String can also be broken, (can iterate objects)
To give a sequence, a list, in the argument position, to iterate over an object preceded by a * means to break the sequence in order, the table in the position of the formal parameter to combine the received parameters into a tuple, if it is a dictionary, then also can be scattered. But it will take two of them *
def Fun (* *Kwargs) :print= {'a'b ': 2}fun (**dic)
python--Primary Knowledge function