Home > Backend Development > Python Tutorial > Deep understanding of code indentation rules in python

Deep understanding of code indentation rules in python

青灯夜游
Release: 2022-07-01 10:16:23
forward
4969 people have browsed it

Python has strict requirements for indentation. The following article will give you an in-depth understanding of the code indentation rules in python. I hope it will be helpful to you!

Deep understanding of code indentation rules in python

Python uses indentation to distinguish different code blocks, so it has strict requirements for indentation.

1. The indentation does not comply with the rules. The parser will report an indentation error and the program cannot run.

Deep understanding of code indentation rules in python

#2. Depending on the indentation, the effect of program execution may also be different.

For example, in the code in the picture, the code on the left will print line 2, while the code on the right will not print lines 1 and 2

Deep understanding of code indentation rules in python


, Code indentation rules

Indentation is for logical lines, so you must first distinguish between physical lines and logical lines in the code.

Physical lines: The code displayed in the code editor, each line is a physical line.

Deep understanding of code indentation rules in python

Logical line: The Python interpreter interprets the code, and a statement is a logical line.

Deep understanding of code indentation rules in python


In python code

1. You can use ";"# The ## sign combines multiple logical lines into one physical line.

Deep understanding of code indentation rules in python

##**There is a premise here, more logical lines should belong to the same code level.

# Branch else code blocks like this cannot be connected directly using ";". 2. You can use

"\"

to wrap a logical line and write it as multiple physical lines.

Deep understanding of code indentation rules in python#3. Variable assignment statements such as dictionaries and lists can be directly wrapped and written as multiple physical lines.

Deep understanding of code indentation rules in python


Indentation rules1. The "first line" of a logical line requires a top space , that is, no indentation (that is, the first logical line of a source code)

Deep understanding of code indentation rules in python2. The same logical layer (the same code block) maintains the same indentation amount

Deep understanding of code indentation rules in python3,

":"

Mark a new logical layersuch as: while loop, if branch, function declaration, class Definition, etc.

Increase the indentation to enter the next code layer

Decrease the indentation to return to the previous code layer

Deep understanding of code indentation rules in python

2. Indentation amount and indentation characters

Python can use spaces or tab characters (tab characters) to mark indentation. There is no limit to the amount of indentation (number of characters). The

space

and tab characters are usually displayed in blank form. It is difficult to distinguish them when used together, which affects code reading and increases maintenance and debugging. difficulty. Therefore, the Python PEP8 coding specification guides the use of 4 spaces as indentation.

Deep understanding of code indentation rules in pythonIn actual development, the code size is large and the indentation depth is affected. 2 spaces will be selected as the indentation. , easier to read.



** Some friends in the comments have doubts about "mixing tab characters and spaces". The key is that they do not understand the logic layer or code block. The ""can be mixed"" mentioned here is based on the premise. To be precise, different logical layers can use different indentation characters.

【Related recommendations: Python3 video tutorial

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

Related labels:
source:csdn.net
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