About indentation Python, we understand what?

  Python is a unique language, code block by its indentation (the Indentation) to mark (most languages ​​are used as a marker braces code block), the same having a plurality of indented lines belong to the same code Piece. If the code is indented inexplicable chaos, Python interpreter will report an error, and therefore Pytho has also been dubbed "the need to hold a vernier caliper language learning."

Indent Tab Python is replaced by four spaces, it is possible to use four indented spaces, but in the actual programming, it is best not to use the spacebar to indent (error-prone).

Use indentation in Python, you need to pay attention to the following points:

  1. Do not use the Tab key to indent ( should not indent indent ) should not use the Tab key to use in place of a space

For example:         the use of indentation in the wrong place using the indentation, an error will occur.

  2. Observe indent extent, to varying degrees at different levels of the code indentation

For example , two different print statements indented, so their level is different, and some statements if conditions are required to meet before they can run.

 

 

 

  3. not recommended indented with spaces (four spaces to indent a), the amount of indentation wrong

To Sublime Text, for example, the following two lines of code indentation, it looks the same, but in fact the second indent statement is illegal!                                         Because one less space, resulting in the wrong program. It is recommended to use Tab to indent using spaces may be unexpected errors because the number of spaces does not occur. 

  4. Do not confuse Tab and space, the actual process of how to see it?

In Sublime Text3 example, the following two indented lines of code, looks the same, and the space is actually Tab, are not identical , wherein the spaces appear as small dots, tTab displayed as a straight line.

 

  Python indentation is the soul, the rational use of indentation, making the code aesthetically pleasing, sometimes too much indent, will be dazzling. So we have to use reasonable indent, to write high-quality code.

Guess you like

Origin www.cnblogs.com/Y-xp/p/11598022.html