Home >Backend Development >Python Tutorial >What is indentation in python

What is indentation in python

藏色散人
藏色散人Original
2019-07-05 10:18:0017403browse

What is indentation in python

What is indentation in python

Strict code indentation is a major feature of python grammar. Just like the curly braces in the C language family (C, C, Java, etc.), they are as important as they are necessary in most situations. In many code specifications, there are also requirements for code writing to be line-wrapped and code indented according to certain rules, but these requirements are purely for the convenience of people (programmers) to read, use or modify. For compilers or interpreters , completely turning a blind eye.

But for the Python interpreter, the indentation before each line of code has grammatical and logical meaning. This feature of Python often causes controversy among Python users and non-Python users.

In fact, Python’s forced code indentation is like a double-edged sword, with advantages and disadvantages. Obviously, the advantage is that under the strict requirements of code indentation, the code is very neat and standardized, pleasing to the eye, which improves readability and maintainability to a certain extent.

However, Python has strict code indentation. For people who have transferred from other languages, since the first language for computer and related majors is generally C/C or Java, their grammatical styles are basically the same. , so it may take some time to get used to it. Python code indentation is very strict. If the code is not written according to its rules, syntax errors may occur, such as unexpected indent, and sometimes logical errors may occur.

Using C language as a comparison:

The following code segment:

    if(True)
    {
        x=1;//语句1
        y=2;//语句2
    }

In C language, curly brackets "{}" are used as the identifier of the code block, and the above " Statement 1" and "Statement 2" are the code blocks belonging to the "if" conditional statement. In the braces, there is no need to consider the indentation of the statement. As long as the statement is in the braces of "if", it belongs to the code of "if" piece. But in order to improve the readability of the code, well-formatted indentation is generally required!

In python, when syntax errors or logic errors occur due to code indentation, there are generally two situations: one is mixing tabs and spaces for indentation, and the other is the editor handles indentation differently. Here is an example, the code is like this:

    def test():
    ——if True:
    ————print("a")
    ——else:
    ————print("b")
   .....print("c")

In the above code, I use "——" to represent a "TAB" key, and "." to represent a space. Under normal circumstances, you will think that the above will output a, but actually enter ac. This is what happens when using an editor such as Notepad. The reason is that the TAB key is regarded as 4 spaces by the editor, so the code seems to be correct. After being translated by the editor, it no longer belongs to the same code block because the indentation is different! However, in pycharm, according to the above writing method, the expected results can be output. This may be because pycharm treats a TAB as two spaces.

Related recommendations: "Python Tutorial"

The above is the detailed content of What is indentation in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What does python fd mean?Next article:What does python fd mean?