Python Compound statement

Source: Internet
Author: User

Python Statement

Simple statement

A simple statement has a (logical) line.

1. Expression statements

An expression can also be a statement. This is especially useful if an expression is a function call or a document string.

For example:

"This modue contains spam-related function."

2. Assertion statements

an assertion statement can check if the condition is true, or if it is not true, raises a Assertionerror(optional supplied error message) exception

For example:

Assert age >=12, ' children under ' is not allowed '

3. Assignment statements

An assignment statement binds a variable to a value. Multiple variables can be assigned at the same time (through the queue unpacking), the assignment can also be chained.

For example:

x = # Simple Assignment

Name, age = ' Gumby ', # sequence unpacking

x = y = z = ten # chain Assignment

4. Incremental assignment Statements

An assignment can also be augmented by an operator, which can already have a variable value and a new value, and then the variable will be re-bound to the result. If the original value is mutable, it is modified (the variable is still bound to the initial value).

5.pass Statements

Pass statement is a "no action", that is, nothing to do. He can act as a placeholder, or in a function where you do not need to do anything, as the only statement in a block that requires a syntactic structure.

For example:

Try:x.name

Except Attributeerror:pass

Else:print ' Hello ', x.name

6.del Statements

The Del statement unlocks the binding of the variable and attribute and removes a part (position, slice, or storage slot) in the data structure (map or sequence). He cannot be used to delete values directly, because the value can only be deleted by garbage collection.

For example:

del x unbind the variable

del seq[42] Deleting sequence elements

Del seq[42:] Delete sequence slices

del map[' foo '] delete a mapping entry

7.print Statements

The Print statement writes one or more values (automatically formatted with str , separated by a single space) to the given stream, and defaults to sys.sdtout. Unless the print statement ends with a comma, it adds a newline character at the end of the string that is written.

print ' Hello, world! ' # write ' Hello, world\n ' to sys.stdout

Print 1, 2, 3 # writes ' 1 2 3\n ' to sys.stout

Print >>somefile, ' xyz ' # writes ' xyz ' to Somefile

Print # writes ' sys.stout ' to the

8.return Statements

The return statement terminates the function's run and returns a value. If no value is provided, none is returned .

For example :

Return # returns None from the current function

return # from the current function

Return 1, 2, 3 # returns from the current function (1, 2, 3)

9.yield Statements

The yield statement temporarily terminates the execution of the generator and generates a value. A generator is a form of an iterator that can be used with other objects for a for Loop.

For example:

Yield is returned from the current function 42

10.raise Statements

The raise statement throws an exception. A call can be made without arguments ( except the currently caught exception within the clause of the argument), or it can subclass Exception and provide optional arguments (in this case, Constructs an instance), or uses An instance of the Exception subclass.

For example:

Raise # can only be used in except clauses

Raise Indexerror

Raise Indexerror, ' index out of bounds '

Raise Indexerror (' Index out of Bounds ')

11.break Statements

The break statement ends the current loop statement ( for or while) and immediately executes the statement after the loop.

For example:

While True:

line = File.readline ()

If not line:break

Print Line

12.cotinue Statements

The Cotinue statement is similar to the break statement, which also terminates the iteration in the current loop, but does not completely terminate the loop, but continues from the beginning of the next iteration process.

For example:

While True:

line = File.readline ()

If not line:break

If Line.isspace (): Continue

Print Line

13.import Statements

The import statement is used for importing names from external modules (variables bound to functions, classes, or other values). This also includes the from_future_import ... statement. This statement is used to import features in future versions of Python .

For example:

Import Math

From math import sqrt

From math import sqrt as SquareRoot

From Math Import *

14.global Statements

The global statement is used to mark a variable as a global variable. It can be used within a function to allow statements in the body of a function to rebind global variables. Using the global statement is generally considered a bad programming style and can be avoided as much as possible.

For example:

Count = 1

Def Inc ():

Global Count

Count + = 1

15.exec Statements

The EXEC statement is used to execute A string containing a Python statement, optionally given a global and local namespace (dictionary).

For example:

exec ' print ' Hello, world '

exec ' x = 2 ' in Myglobals. Mylocals #myglobals and mylocals are dictionaries .

Python Compound statement

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.