Python function Chapter
Functions are re-elected, they allow a name to be given to a block of statements, and can then be run anywhere in your program using the name of this statement, any number of times. This is called the call function. For example, there are many efficient functions such as Len and range built in.
The function uses the DEF keyword to define the identifier name followed by a function after the Def, followed by a parenthesis, which can include some variable names that end with a colon: the next is a statement called the function body.
Def SayHello (): print "Hello world!" SayHello ()
function formal Parameters
The value of a function parameter is the value you provide to the function, so that you can do something with these values, which are like variables except that their values are defined when we call the function and not within the function itself.
Parameters are specified within the parentheses of the function definition, separated by commas when we call the function, we need to provide the value in the same way.
Note: The parameter name in the function is a formal parameter, and the value of the function call you provided becomes an argument
def Printmax (b): #a B is the formal parameter if a>b: print A, "is maximum" else: print B, "is Maximum" Printmax ( 3,4) #23 4 is the argument X=5y=7printmax (x, y) #x y is the argument
Local variables
Local variables are variables that are declared within a function definition, and they have no relation to the same other variables outside the function, that is, the variable name is local to the function, and this becomes the scope of the variable. The scope of all variables is that they are defined fast, starting at the point where their name is defined
def func (x): print "x is", x x=2 print "Change local x to", x # This x has a value of 2 is the local variable can only be used inside the function X=50func (x) print "X is still", the X #而这个X定义的值为50 is outside the function so called global variable
Using the global statement
If you want to assign a value to a variable that is defined outside the function, then you have to tell Python that the variable name is not local, but the global
Of We use the global statement to complete this function. without the global statement, it is impossible to assign a value to a variable that is defined outside the function.
def func (): Global x print "x is", x x=2 print "Change the Locak", Xx=50func () print "Value of x is", X
Defining variables requires using the keyword global to define the X 2 defined in the above code (although defined in the function body but still global variables can continue to be used outside the function body), so the last printed value is 2.
A convenient parameter method in Python is the default parameter value, and sometimes we do not want to assign a value to each parameter when we declare the function, so we can use the default parameter value at this time. You can assign default parameter values to parameters by adding the assignment operator (=) and the default value after the parameter name of the function definition
DEF say (message, times = 1): Print message * Timessay ("Hello") Say ("World", 5)
A default parameter value is defined by the Say function (two parameters defined)
When called, it provides a hello argument to a message without passing in TIME's formal parameter, so when called, the default is to use 1 to print the Hello
The next line provides two parameters to get the world's output 5 times.
Note: The theory is that the default parameter value should be a parameter more accurate to make the value should not be able to change that is not mutable
Key parameters
If your function has many parameters and you want to specify only one or a few parameters, you can assign values to these parameters by name, which is called the key parameter
This has two advantages: since we don't have to worry about the order of the parameters, it's easier to use the function, and assuming that the other parameters have default values, we can assign only those parameters that we need.
def func (A, b=5,c=10): # b C is defined by the default value of the parameter print "A is", A, "and" are ", B," and C is ", Cfunc (3,7) func (25,c=24) func (c= 50,A=100)
Output Result:
======================== restart:c:/python27/func.py ========================
A is 3 and B are 7 and C is 10
A is, and B is 5 and C is 24
A is and B are 5 and C is 50
>>>
We can see that if we don't specify a specific parameter, the system defaults to match the formal parameter, i.e. A=3 b=7 C uses the default value of 10
Func (25,c=24) passes 25 to formal parameter a B using the default parameter 5 C to use the argument value
Func (c=50,a=100) Note that this order is free to define this order, which is very different from other languages, which is also a convenient place to change languages.
Return statement
The return statement is a function returned from a function, and we can also choose to return a value from the function
def maximum (x, y): if x>y: return x else: The value that appears after running the result is 3, passing in 2 and 3 to X and Y and then running if Else statement return y value 3
One thing to be reminded here is return in Python, and return with no return value is equivalent to return none. None represents a special type of nothing in Python for example, if the value of a variable is none, it means that he has no value.
Unless you provide your own return statement, the end of each function implies return none, and we can understand a little by using the following function.
def maximum (x, y): if x>y: return x elif x = = y: return x+y else: return y print maximum ( 3,10)
======================== restart:c:/python27/func.py ========================
10
>>>
Program return value is 10
If we change the last else execution statement to print Y The result would be as follows:
======================== restart:c:/python27/func.py ========================
10
None
>>>
The default is to execute a hidden return none statement when no return is used
DocString
Python has a fantastic feature, called a document string, which is often referred to as docstrings. Docstrings is an important
Tool, as it helps your program document to be easier to understand, you should try to use it. You can even run the program from the
function to recover the document string!
1 defPrintmax (x, y):2 """Prints The maximum of the numbers.3 4 The values must be intergers."""5x=int (x)6y=Int (y)7 8 ifX>y:9 PrintX"is maximum"Ten Else: One PrintY"is maximum" A - -Printmax (3,5) the PrintPrintmax.__doc__ - -Help (Printmax)
The string in the first logical line of the function is the document string for this function. Note that Docstrings also applies to modules and classes.
The Convention for a document string is a multiline string whose first line begins with an uppercase letter and ends with a period. The second line is a blank line, from the third row
The beginning is a detailed description. It is strongly recommended that you follow this convention when using a document string in your function.
You can use __DOC__ (note double underline) to call the document string property of the Printmax function (the name of the function). Please
Remember that Python takes everything as an object, including this function. We'll learn more about objects in the later class chapter.
Knowledge
If you've already used help () in Python, you've seen the use of docstings! All it does is grab the function
The __doc__ attribute and then neatly show it to you. You can try it on the function above--just package it in your program.
including Help (Printmax). Remember to press Q to exit help.
Automation tools can also extract documents from your program in the same way. Therefore, I strongly recommend that you compile any formal functions that you have written
Writes a document string. Use docstrings similar to help () with the Pydoc command that came with your Python distribution.
===================== restart:c:\python27\docstring.py =====================
5 is maximum
Prints the maximum of the numbers.
The values must be intergers.
Help on function Printmax in module __main__:
Printmax (x, y)
Prints the maximum of the numbers.
The values must be intergers.
>>>
Python Basic Learning 1---functions