Python's general format-indentation

Reference link: Statements, indentation and comments in Python

Python's syntax is relatively simple, using indentation, the code written is like the following:

# print absolute value of an integer:

a = 100

if a >= 0:

print(a)

else:

print(-a)

Statements beginning with # are comments. Comments are for people to see and can be any content. The interpreter will ignore the comments. Each other line is a statement. When the statement ends with a colon:, the indented statement is regarded as a code block.

There are pros and cons to indentation. The advantage is that it forces you to write formatted code, but it does not specify whether the indentation is a few spaces or a Tab. In accordance with conventional management, an indentation of 4 spaces should always be used.

Another benefit of indentation is that it forces you to write less indented code. You will tend to split a long piece of code into several functions, resulting in less indented code.

The downside of indentation is that the "copy-paste" function fails, which is the most cheating. When you refactor the code, the pasted code must be rechecked for correct indentation. In addition, it is difficult for IDEs to format Python code like Java code.

Finally, please be aware that Python programs are case sensitive. If you write a wrong case, the program will report an error.

summary

Python uses indentation to organize code blocks. Please be sure to follow the customary habits and stick to the indentation of 4 spaces.

In the text editor, you need to set the automatic conversion of Tab to 4 spaces to ensure that Tab and spaces are not mixed.

Reprinted at: https://www.cnblogs.com/xuhaojun/p/9160705.html

Recommended Posts

Python's general format-indentation
python's @abstractmethod