My friends, for reprint please indicate the source: blog.csdn.net/jiangjunsho…

Disclaimer: During the teaching of artificial intelligence technology, many students asked me some python related questions, so in order to let students master more extended knowledge and better understand AI technology, I asked my assistant to share this Python series of tutorials, hoping to help you! Since this Python tutorial is not written by me, it is not as funny and boring as my AI teaching. But its knowledge points or say in place, also worth reading! If you want to learn AI technology, you can click on my teaching website. PS: if you don’t understand this article, please read the previous article first. Step by step, you won’t feel difficult to learn a little every day!

Python detects block boundaries as line indentation, which is the empty space to the left of the program code. All statements indented the same distance belong to the same block of code. In other words, statements within a block are aligned vertically, as if within a column.

The following illustrates the block structure of the following program code.

X = 1 if x: y = 2 if y: print ('block2') print ('block1') print ('block0')Copy the code

This code consists of three modules: the first (the top-level code of the file) is not indent at all, the second (inside the outer if statement) is indent by four, and the third (the print statement under the nested IF) is indent by eight.

In general, top-level (unnested) code must start in column 1. Indentation can consist of arbitrary Spaces and tabs, as long as all statements in a particular single block are the same. In other words, Python doesn’t care how you indent your code, only if the indentation is consistent.

The following code snippet shows indentation errors in Python code:

X = 'SPAM' # Error: first line indented if 'rubbery' in 'shrubbery': print (x * 8) x += 'NI' # Error: Unexpected indentation if x. edds with ('NI') : x *= 2 print (x) # Error: Inconsistent indentCopy the code

The correct indent version of this code looks like this:

X = 'SPAM' if 'rubbery' in 'shrubbery': print (x * 8) x += 'NI' if x. edds with ('NI') : X *= 2 print (x) # SPAMNISPAMNICopy the code

One of the primary uses of whitespace in Python is for indentation on the left side of code. In most other environments, you can add white space or no space to your program code. Indentation is actually part of Python syntax, not just programming style: all statements in any particular single block must be indent to the same level or Python will report syntax errors. This is intentional because you don’t need to explicitly identify the beginning and end of a nested block of code, and some of the syntactic clutter that is common in other languages isn’t visible in Python.

Making indentation part of the syntax model also reinforces consistency, which is an important part of readability in structured programming languages like Python. Python’s syntax is described as “what you see is what you get” — the unambiguous indentation of each line of program code tells the reader where it belongs. This consistent look and feel makes Python programs easier to maintain and reuse.

Avoid mixing tabs and Spaces. Although you can indent with Spaces or tabs, it’s not good to mix the two in a block of code. The code also works if you mix tabs and Spaces. However, such code is difficult to modify. Worse, mixing tabs and Spaces can make code hard to read — tabs can look very different in another programmer’s editor than they do in yours.