current position:Home>Python series tutorial 132 -- why use indentation syntax

Python series tutorial 132 -- why use indentation syntax

2022-02-02 00:44:57 AI technology of artificial intelligence

friends , If you need to reprint, please indicate the source :juejin.cn/post/703842…

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 !

For used classes C For language programmers , The indentation rule may seem a little special at first glance , But this is exactly Python Well designed features , yes Python Force programmers to write unified 、 One of the main ways of neat and readable programs . This means that you have to follow the logical structure of the program , Organize the program code in a vertical alignment . The result is a more consistent and readable program .

Aligning the code according to the logical structure is the main part of making the program readable , Therefore, it has reusability and maintainability , For yourself and others . Even if you don't use it Python, You should also align the code in any block structured language to make the program more readable .

When I was doing full-time basic development, I handled the problems of many programmers C++ Program . It is almost inevitable that , Every programmer has his own style of indenting code . for example , The first person to write code is indented with four spaces :

while (x > 0) {

    --------;

    --------;
 Copy code 

This man later squeezed into the management , And someone who likes to indent a little more to the right to take his place :

while (x > 0) {

    --------;

    --------;

           --------;

           --------;
 Copy code 

The man then met other opportunities , And someone who takes over this code likes to indent less :

while (x > 0) {

    --------;

    --------;

           --------;

           --------;

--------;

--------;

}
 Copy code 

In any code block structured language , Whether it's Python Or other languages , If the indentation of nested code blocks is inconsistent , They will be difficult to explain 、 Modify or reuse , Because code can no longer visually reflect its logical meaning . Readability is very important , Indentation is the main element of readability .

If you use class C Language has written a lot of programs , Maybe you've had a headache for the following example :

if (x)

     if (y)

          statement1;

else

    statement2;
 Copy code 

This else Which one does it belong to if What about ? It's amazing that , This else It belongs to nested if sentence [if (y)], Even if it looks like it belongs to the outer layer if (x) Of . This is a C The classic trap in language , Moreover, it may cause the code modifier to completely misunderstand the code and modify it in an incorrect way, and the reason has not been found , Until a huge mistake is made ! This kind of thing is Python It can't happen in the future : Because indentation is important , What a program looks like means how it will run . Look at the following Python sentence :

if x:

     if y:

          statement1

else:

    statement2
 Copy code 

In this case ,else Vertically aligned if Is its logical if( The outer if x). In a sense ,Python yes WYSIWYG Language —— What you see is what you get (what you see is what you get). Because whoever wrote it , What a program looks like is how it runs .

There is no absolute standard for indentation : It is common to have four spaces or one tab per layer , But how and how much you want to indent is up to you . The deeper the nesting, the more indented the code block to the right , The shallower it is, the closer it is to the previous block . But remember , It shouldn't be in the same paragraph Python Mix tabs and spaces in the code .

No matter what language you write code in , Should be indented consistently to maintain readability , This point cannot be overemphasized . actually , If you haven't learned how to do this in your previous career , Then your teacher left you hurt . He should have taught you that !

copyright notice
author[AI technology of artificial intelligence],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202020044547950.html

Random recommended