Python indentation and Process Control statements

Source: Internet
Author: User

1. Indent: Indentation is the only way Python represents a block of statements;

All statements in a block of statements must use the same indentation, representing a contiguous sequence of logical lines;

The first line of the source file does not need to be indented (it is not allowed to start with any spaces);

The standard Python style is to use 4 spaces per indentation level;

How to represent a logical line across rows:

(1) ' \ ' can connect two adjacent physical lines into a logical line;

(2) [], {}, () can cross the physical line;

(3) Triple quotation string constants (including single and double quotes) can also span multiple lines, but one thing must be noted: when wrapping, you need to add the ' \ ' slash at the end of the physical line, otherwise the newline character will be included;

2.if statement:

The basic form is:

If judging condition:
Execute statement ...
Else
Execute statement ...

3.while Loop statement:

The basic form is:

While judging condition:
Execute statement ...

4.for Loop statement:

The basic form is:

For Iterating_var in sequence:
Statements

5.break statement:

Used to terminate the loop statement, that is, the loop condition does not have a false condition or the sequence has not been fully recursive, will also stop the execution of the loop statement;

6.continue statement:

Used to tell Python to skip the remaining statements of the current loop, and then proceed to the next round of loops;

The difference between range and xrange:

The xrange usage is exactly the same as range, and the difference is not a list object, but a generator.

When generating a large sequence of numbers, using xrange will be much better than range performance, because there is no need to open up a large amount of memory space.

Python indentation and Process Control statements

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.