Python notes (4)

Source: Internet
Author: User

Function

1. Function Introduction

A function is an important part of a program. They allow you to give a statement a name, and then you can use this name to run the statement block multiple times anywhere in your program.

In python,

Function PassdefKeyword definition. The def keyword is followed by a Function Identifier name, followed by a pair of parentheses. Variable names can be included in parentheses, which end with a colon. Next is a statement, which is the function body. The following example shows that this is actually very simple:

#! /Usr/bin/python <br/> # filename: function1.py </P> <p> def sayhello (): <br/> Print 'Hello world! '# Block belonging to the function </P> <p> sayhello () # Call the function </P> <p>

Ii. Parameters

In the process of using the function, it is completed by passing the form parameters and real parameters.

For example:

#! /Usr/bin/python <br/> # filename: func_param.py </P> <p> def printmax (A, B): <br/> If A> B: <br/> Print A, 'is maximum' <br/> else: <br/> Print B, 'is maximum' </P> <p> printmax (3, 4) # directly give literal values </P> <p> X = 5 <br/> Y = 7 </P> <p> printmax (x, y) # Give variables as arguments </P> <p>

Output:

$ Python func_param.py <br/> 4 is maximum <br/> 7 is maximum

Iii. Local Variables

When you declare variables in the function definition, they have nothing to do with other variables with the same name outside the function, that is, the variable name is local for the function. This is called the scope of a variable. The scope of all variables is the block they are defined, starting from the point where their names are defined.

#! /Usr/bin/python <br/> # filename: func_local.py </P> <p> def func (x): <br/> Print 'x ', x <br/> X = 2 <br/> Print 'changed local X to ', x </P> <p> X = 50 <br/> func (X) <br/> Print 'x is still', x </P> <p>

Output:

$ Python func_local.py <br/> X is 50 <br/> changed local X to 2 <br/> X is still 50

Global statements

If you want to assign a value to a variable defined outside the function, you have to tell Python that the variable name is not local, but global.

You can use the value defined outside the function (assuming there is no variable with the same name in the function ). However, this is not conducive to the readability of the Code.

#! /Usr/bin/python <br/> # filename: func_global.py </P> <p> def func (): <br/> global x </P> <p> Print 'x is ', x <br/> X = 2 <br/> Print 'changed local X ', x </P> <p> X = 50 <br/> func () <br/> Print 'value of X is ', X

Output:

$ Python func_global.py <br/> X is 50 <br/> changed global X to 2 <br/> value of X is 2

Note: You can use the sameglobalThe statement specifies multiple global variables. For exampleglobal x, y, z.

Iv. default parameter values

For some functions, you may want some of its parameters to be optional. If you do not want to provide values for these parameters, these parameters use the default values. This function is completed by default parameter values. You can add the value assignment operator (=) and default value after the parameter name defined by the function to specify the default parameter value for the parameter.

For example:

#! /Usr/bin/python <br/> # filename: func_default.py </P> <p> def say (message, Times = 1 ): <br/> Print message * Times </P> <p> say ('hello') <br/> say ('World', 5) </P> <p>

Output:

$ Python func_default.py <br/> Hello <br/> worldworldworldworld

Note: Only those parameters at the end of the parameter table can have default parameter values. That is, when you cannot declare a function parameter, you must first declare a parameter with the default value and then declare a parameter without the default value.
This is because the value assigned to the form parameter is assigned based on the position. For example,def func(a, b=5)Is valid,def func(a=5, b)Is invalid.

5. Key Parameters

If a function has many parameters and you only want to specify a part of them, you can assign values to these parameters by name. This is called a key parameter. We use the name (keyword) instead of specifying the real parameter for the function (the method we used previously.

This method has two advantages: because we do not have to worry about the order of parameters, it is easier to use functions. 2. If other parameters have default values, we can only assign values to those parameters we want.

For example:

#! /Usr/bin/python <br/> # filename: func_key.py </P> <p> def func (a, B = 5, c = 10 ): <br/> Print 'a is ', A,' and B is ', B, 'and C is', C </P> <p> func (3, 7) <br/> func (25, c = 24) <br/> func (C = 50, A = 100) </P> <p>

Output:

$ Python func_key.py <br/> A is 3 and B is 7 and C is 10 <br/> A is 25 and B is 5 and C is 24 <br/> A is 100 and B is 5 and C is 50

Vi. Return Statement

#! /Usr/bin/python <br/> # filename: func_return.py </P> <p> def maximum (x, y): <br/> If x> Y: <br/> return x <br/> else: <br/> return Y </P> <p> Print maximum (2, 3) </P> <p>

Output:

$ Python func_return.py <br/> 3 </P> <p>

VII. Document strings

Python has a wonderful feature called a document string, which is usually referred toDocstrings. Docstrings is an important tool, because it helps your program documentation to be easier to understand, you should try to use it. You can even restore the document string from the function when the program is running.

#! /Usr/bin/python <br/> # filename: func_doc.py </P> <p> def printmax (x, y ): <br/> ''' prints the maximum of two numbers. </P> <p> the two values must be integers. ''' <br/> X = int (x) # convert to integers, if possible <br/> Y = int (y) </P> <p> If x> Y: <br/> Print X, 'is maximum' <br/> else: <br/> Print y, is maximum '</P> <p> printmax (3, 5) <br/> Print printmax. _ Doc __

Output:

$ Python func_doc.py <br/> 5 is maximum <br/> prints the maximum of two numbers. </P> <p> the two values must be integers.

The string in the first logical line of the function is the document string of the function. Note that docstrings also applies to modules and classes.

A document string is a multi-line string whose first line starts with an uppercase letter and ends with a full stop. The second line is empty, and the detailed description starts from the third line. Follow this Convention when using document strings.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.