What does Python use to distinguish different statement blocks?

青灯夜游
Release: 2020-07-24 15:14:15
Original
5807 people have browsed it

Python uses indentation format to distinguish different statement blocks. The Python language uses indentation to indicate the beginning and exit of a statement block (off-side rule). Increasing indentation indicates the beginning of a statement block, while decreasing indentation indicates the exit of a statement block.

What does Python use to distinguish different statement blocks?

Python statement block

1. The statement block is executed or executed when the condition is true (conditional statement) A group of statements multiple times (loop statements);

2. Place a space before the code to indent the statement to create a statement block. Each line in the statement block must be indented by the same amount;

3. Indentation: Python developers intentionally prevent programs that violate indentation rules from being compiled, in order to force programmers to develop good programming habits;

4. Python language uses indentation Indicates the beginning and exit of a statement block (Off-side rule), rather than using curly braces or certain keywords;

5. Increasing indentation indicates the beginning of a statement block, while decreasing indentation indicates the statement Block exit.

6. Indentation is part of Python syntax.

The following pseudocode (not real Python code) shows how indentation works:

    this is a line
    this is a line
         this is another block
         continuing the same block
         the last line of this block
    phew,there we escaped the inner block
Copy after login

7. Many languages ​​use special words or characters (such as begin or { ) to represent a The beginning of a statement block, and another word or character (such as end or }) is used to indicate the end of the statement block.

8. But in Python, the colon (:) is used to identify the beginning of a statement block, and each statement in the block is indented (the indentation amount is the same).

9. When the indentation amount is the same as that of the closed block, it means that the current block has ended.

Explanation:

Python’s statements are different from programming languages ​​such as C. There is no need to add a semicolon at the end of a Python statement to indicate the end of the statement, just wrap it in a new line. Another very important point is to use indentation to indicate the logical relationship between statement blocks instead of braces. These two features not only maintain code readability, but also reduce symbol input and improve efficiency. Lazy people like

. The number of indented spaces is variable, but the corresponding statements in the same code block must be consistent, otherwise an error will be reported. In the example code below, an error will occur due to inconsistent indentation.

if True:    print ("True")else:    print ("else")  print ("have a try")
Copy after login

Recommended learning: Python video tutorial

The above is the detailed content of What does Python use to distinguish different statement blocks?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!