August 8, 2018 what to learn: functions
I. Reviewing the contents of the previous lesson
File operation:
Open (file path, mode= "", encoding= "")
Mode:r Read Only w write a append write r+ read/write w+ write a + append write read B process non-text file
Cursor: Seek (0) to bring the cursor to the beginning
Seek (0,2) to pass the cursor to the end
Tell () find the location of the cursor
Truncate () truncation
r+ deep pits, no matter how much you read. Write at the end of the writing.
File modification:
Import OS
With open ("Modified target file", mode= "R", encoding= "Utf-8") as F1,\
Open ("Modified target file _ copy", Mode= "W", encoding= "Utf-8") as F2:
For line in F1:
"' Modify operation '
s= line.xxxxx
F2.write (s)
#删除原来文件
Os.remove ("Modified target file")
Os.rename ("Modified target file _ Copy", "Modified target file")
Two. Homework explanation
Three. Today's main content
1. Function: function is the encapsulation of function
Grammar:
def function name (formal parameter list):
function Body (code block, return)
Call:
Function name (argument list)
2. Return value
Return: At the time of function execution. If you encounter a return. Direct return
Return can terminate a program execution
1. If the function does not write anything, do not write return, there is no return value. Got none.
2. Write return in the middle or end of the function, and return none
3. Write the return value in the function. Returns a value.
4. You can return multiple return values in a function, return value 1, value 2, value 3 ...., the tuple is received
Multiple return values, the result of the printout is a tuple
Define a function: A+b
3. Parameters
The function passes information to the function when it executes.
* Parameter: variable for position of function declaration
The variables in the diagram where tools are declared in a function are a formal parameter
Positional parameters, given by the position of the form parameter.
* Argument: The specific value given when the function is called
Reference: The process of handing an argument to a formal parameter
1. Actual parameters:
1. Position parameters, according to the parameter position of the parameters, the value of the formal parameter
2. Keyword parameters, according to the name of the formal parameter to pass the parameter value
3. Mixing parameters. Use positional parameters, also with key parameters
2. Formal parameters:
1. Position parameter First position parameter after keyword parameter
2. Default value parameter first position parameter after default value parameter
3. Dynamic parameters (Preview preview)
Preview: Dynamic parameters, scope
Yellow sand hundred war wear golden, not broken Python end not yet!