Row structure and indentation in Python

Source: Internet
Author: User
Each statement in the program ends with a newline character you can use the continuation character (\) backslash to divide the long statement into lines

Wather_is_hot  = 1;     Watherer = 0;     if (wather_is_hot ==1) and \      (watherer==0):       print ("The water in the Cup is Hot");

There are two types of cases where a statement does not apply a backslash or can span rows
1, a single statement can span lines such as: with parentheses in parenthesis, you can write more than one line
Cases:

Dict = {      ' title ': ' Walker ',      ' name ': ' Wu Song ',      ' num ':     

2, any program part in three quotation marks can be written in multiple lines no backslash is required to continue the line
Cases:

str = "The first return of the Outlaws of the Marsh: Gao Qiu and the king (later Hui Zong) get acquainted.   was promoted to the temple handsome mansion Qiu, to have knocked his Wang Shengguo son 800,000 death dealers coach Wangjin execution revenge.    Wangjin away Yanan mansion, in the village to give he teach gun stick. He because of the small Chanda and Sasaka, yangspring acquaintance with the exchange.   Huayin County mid-autumn night to the historian village to seize the moon drinking meat history into and less Huashan three heroes ';

Indentation is used to mark different blocks of code such as function body conditional statement loops and classes
The indent of the first statement in the code block is arbitrary, but the indentation in the entire code block must be consistent
Such as:

A = N;    If a:     a + = 1;     A/= 2;     Print (a);  #缩进一致  Correct
If a:      a +=1;        a/=2;      Print (a); #缩进不一致  indentationerror Error

If a function body, a conditional statement, a loop, or a class is shorter and contains only one statement, you can place it on the same line as:

If A>b:print (' OK ');    Else:print (' no ');

To represent an empty function body or code block, you can use the PASS statement such as:

If a:      pass;    else:     print (' Error ');

Because the Python programming community prefers spaces, it is recommended to use a space indent because the system will convert tabs to a 8 times-fold amount of space if you run Python with-t then you will find tabs and spaces mixed in the same program code block. A warning message is displayed using-TT to convert these warning messages to Taberror exceptions

To place multiple statements on a line, use semicolons, separated, if only one statement can end with a semicolon (optional), PS: personal use; end
#字符 in Python that the contents of this line are comments that appear in quotation marks without this effect
In addition, the interpreter ignores all blank lines unless it is run in Python interactive mode (interpreter)
Summarize:
1, line continuation \: Used to separate long program code from the display
A single statement can cross rows without using \
The data within the three quotes can cross the line without using \
2, indent: first statement indent any remaining statements should remain indented consistent with the first statement
3,pass statement: empty function body or code block
4, you can use semicolons; end a statement
5, #代表注释

The above is the structure of the line in Python and indented content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.