definition of a function
functions are passed by assignment, and parameters are passed to the function by assignment. The def statement creates a function object and assigns it to a variable name, and the general format of the DEF statement is as follows:
1 def function_name (arg1,arg2[,...]): 2 Statement 3 [return value]
The return value is not required, and if there is no return statement, Python returns the value none by default.
naming rules for function names: The function name must begin with an underscore or letter and can contain any combination of letters, numbers, or underscores. You cannot use any punctuation , and the function name is case-sensitive. The function name cannot be a reserved word.
The parameters in the function receive the passed values, which can be divided into default parameters, such as:
Tuple (tuples) parameter: def function (*arg)
Dictionary (dictionary) parameter: def function (**arg)
Some function rules:
The default value must be after a non-default parameter, only one tuple parameter (*arg) and one dictionary parameter (**ARG) can be used in a single function definition, and the tuple parameter must be after the connection parameter and the default argument, and the dictionary parameter must be defined last.
Call to function
Defines a function that gives the function a name, specifies the parameters contained in the function, and the code block structure.
Once the basic structure of this function is complete, you can execute it from another function call or directly from the Python prompt.
1 #Defining Functions2 defprintme (str):3 "print any passed-in string"4 Printstr;5 return;6 7 #calling Functions8Printme"I want to invoke the user custom Function!");9Printme"call the same function again");
Return Statement
Return statement [expression] exits the function, optionally returning an expression to the caller. A return statement without a parameter value returns none.
1 defsum (arg1, arg2):2 #returns the and of the 2 parameters.3Total = Arg1 +arg24 Print "function 1:", Total5 returnTotal ;6 7 #Call the SUM function8Total = SUM (10086, 10010 );9 Print "function 2:", total
variables and local variables
Variables defined inside a function have a local scope, which defines the owning global scope outside of the function.
Local variables can only be accessed within their declared functions, while global variables are accessible throughout the program. When a function is called, all variable names declared within the function are added to the scope.
1Total = 0;#This is a global variable2 #Writable Function Description3 defsum (arg1, arg2):4 #returns the and of the 2 parameters.5Total = Arg1 + arg2;#Total Here is the local variable.6 Print "Local Variables:", Total7 returnTotal ;8 9 #Call the SUM functionTenSum (10086, 10010 ); One Print "Global Variables:", total
The above results
1 inside the function is a local variable: the2 function is a global variable: 0
View Code
Python file Operations
1 |
File.close () Close the file. File is no longer read and write after closing |
2 |
File.flush () Refreshes the file internal buffer, directly writes the internal buffer data immediately to the file, rather than passively waits for the output buffer to write. |
3 |
File.fileno () Returns an integer that is a file descriptor (Descriptor FD Integer) that can be used in some of the underlying operations, such as the Read method of the OS module. |
4 |
File.isatty () Returns True if the file is connected to an end device, otherwise False is returned. |
5 |
File.next () Returns the next line of the file. |
6 |
File.read ([size]) Reads the specified number of bytes from the file, if none is given or is negative. |
7 |
File.readline ([size]) Reads the entire line, including the "\ n" character. |
8 |
File.readlines ([Sizehint]) Reads all rows and returns a list, and if given sizeint>0, returns a row with a sum of approximately sizeint bytes, the actual read value may be larger than sizhint because the buffer needs to be populated. |
9 |
File.seek (offset[, whence]) Set the current location of the file |
10 |
File.tell () Returns the current location of the file |
11 |
File.truncate ([size]) Intercepts the file, the truncated byte is specified by size, and the default is the current file location. |
12 |
File.write (str) Writes a string to the file without a return value. |
13 |
File.writelines (Sequence) Writes a list of sequence strings to the file and adds a newline character to each line if a line break is required. |
Python function basics and file manipulation