current position:Home>Python tutorial series 133 -- several special grammars

Python tutorial series 133 -- several special grammars

2022-02-02 04:04:37 AI technology of artificial intelligence

friends , If you need to reprint, please indicate the source…

Statement : During the teaching of artificial intelligence technology , Many students mentioned to me python Related issues , So in order to let the students master more extended knowledge and better understand AI technology , I put the assistant in charge of sharing this python Series of tutorials , I hope that helps ! Because of this python The tutorial wasn't written by me , So it's not as good as mine AI Technology teaching is humorous , It's boring to learn ; But its knowledge is in place , It's also worth reading ! Want to learn AI Technical students can click to jump to my Teaching website .PS: If you don't understand this article, please read the previous article first , Step by step, learn a little everyday, and you won't find it difficult !

In the last article , We mentioned that python Grammar has the following two rules : • The end of a line is the end of the line statement ( You don't need a semicolon ). • Use indentation to represent the code block of nested statements ( You don't need braces ).

however Python Some special rules are also provided to adjust the termination of statements and the code blocks of nested statements .

although Python Statements are usually one line at a time , however Python It is also possible to squeeze a line into multiple statements , They are now separated by semicolons :

a = 1; b = 2; print(a + b) # Three statements on one line
 Copy code 

however , Only when the statements put together are not compound statements themselves . let me put it another way , You can only put simple statements together . for example , Assignment operation 、 Printing and function calls . Compound statements must still appear on their own lines .

Another special rule is the opposite —— You can make the scope of a statement span multiple lines . To achieve this , You just need to enclose the statement with a pair of parentheses : Brackets ()、 square brackets [] Or dictionary braces {} Will do . Any program code enclosed in these symbols can span several lines . The statement will always run , until Python The line containing closed parentheses was encountered . for example , List constants in consecutive lines :

mlist = [111,


 Copy code 

Because the program is enclosed in square brackets ,Python It will then run the next line , Until you meet the closed square brackets . A dictionary contained in curly braces can also span several lines in this way , Parentheses generally deal with tuples 、 Function calls and expressions . Indenting consecutive lines doesn't matter , But to make the program readable , Those lines should also be aligned .

Parentheses can contain everything —— Because any expression can be included , Just insert a left parenthesis , You can go to the next line and write your sentence .

X = (A + B +

     C + D)
 Copy code 

This technique also applies to compound statements . No matter where you need to write a large expression , Just enclose it in parentheses , You can continue on the next line :

if (A == 1 and

    B == 2 and

    C == 3):

           print('spam' * 3)
 Copy code 

In fact, there is an old rule that allows us to span several lines —— When the previous line ends with a backslash , You can continue on the next line :

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

    C + D
 Copy code 

But this method is out of date , At present, to some extent , This method is no longer advocated , Because it's difficult to pay attention to and maintain backslashes , And this approach is quite fragile ( There may be no space after the backslash ).

There is another special case , The body of a compound statement can appear in Python After the colon on the first line of .

if x > y: print(x)
 Copy code 

So we can edit a single line if sentence 、 Single line cycle, etc . however , Only if the compound statement itself does not contain any compound statements , To do this . in other words , Only simple statements can follow a colon , For example, assignment operation 、 Print 、 Function calls, etc . More complex statements still have to be placed on their own lines .

Although there are so many special cases mentioned above , But if you put all statements on separate lines and always indent nested code blocks , Then the program code will be easier to read and easy to modify later .

copyright notice
author[AI technology of artificial intelligence],Please bring the original link to reprint, thank you.

Random recommended