My friends, for reprint please indicate the source: blog.csdn.net/jiangjunsho…

Disclaimer: During the teaching of artificial intelligence technology, many students asked me some python related questions, so in order to let students master more extended knowledge and better understand AI technology, I asked my assistant to share this Python series of tutorials, hoping to help you! Since this Python tutorial is not written by me, it is not as funny and boring as my AI teaching. But its knowledge points or say in place, also worth reading! If you want to learn AI technology, you can click on my teaching website. PS: if you don’t understand this article, please read the previous article first. Step by step, you won’t feel difficult to learn a little every day!

In the last article, we mentioned that Python syntax has the following two rules: • The end of a line is the end of the line (no semicolons needed). • Use indentation to indicate blocks of code for nested statements (no braces required).

But Python also provides special rules for adjusting statement terminations and nested blocks of code.

Although Python statements are typically one line at a time, it is possible to squeeze more than one statement into a single line, separated by a semicolon:

a = 1; b = 2; Print (a + b) # Three statements on one lineCopy the code

This works, however, only if the statements thrown together are not themselves compound statements. In other words, you can only put simple statements together. For example, assignment operations, printing, and function calls. Compound statements must still appear in their own lines.

Another special rule does the opposite — you can make a statement span multiple lines. To do this, all you need to do is surround the statement with a pair of parentheses: parentheses (), square brackets [], or dictionary braces {}. Any program code enclosed in these symbols can span several lines. The statement runs until Python hits the line that contains the closing parentheses. For example, consecutive rows and columns list constants:

mlist = [111,

         222,

         333]
Copy the code

Because the program is enclosed in square brackets, Python runs on the next line until a closing square bracket is encountered. Dictionaries contained in curly braces can also span several lines in this way, while parentheses generally handle tuples, function calls, and expressions. Indentation of consecutive lines is fine, but for readability, those lines should also be aligned.

Parentheses can contain anything — since any expression can be included, just insert a left parenthesis and you can continue your statement on the next line.

X = (A + B + C + D)Copy the code

This technique also applies to compound statements. Wherever you need to write a large expression, just enclose it in parentheses and continue on the next line:

If (A == 1 and B == 2 and C == 3) : print ('spam' * 3)Copy the code

There is an old rule that allows us to span several lines — if the previous line ends with a backslash, we can continue on the next line:

X = A + B + \            # An error-prone alternative

    C + D
Copy the code

However, this method has become outdated, and in some ways it is no longer recommended because of the difficulty and fragility of following and maintaining backslashes (there may be no space after backslashes).

There is also a special case where the body of a compound statement can appear after the colon on the first Python line.

If x > y: print (x)Copy the code

This allows us to edit single-line if statements, single-line loops, and so on. However, this can only be done if the compound statement itself does not contain any compound statements. That is, only simple statements can follow the colon, such as assignment operations, printing, function calls, and so on. More complex statements must still be placed in their own lines.

With all the special cases mentioned above, if you put all statements on separate lines and always indent nested code blocks, your code will be easier to read and modify later.