This article brings you relevant knowledge about python, which mainly introduces the basic coding specifications of Python, including declaring the coding format, indentation rules, comment parts and use of blank lines, etc. ,I hope everyone has to help.
Recommended learning: python tutorial
Coding specification
It exists in various programming languages, and it may not be very intuitive in some languages.
If you are a novice learning to write code, then memorizing the coding rules at the beginning will be helpful for future writing specifications. The impact is huge!
Let’s briefly introduce some coding standards that beginners should keep in mind. They are divided into several aspects. Let’s take a look!
Python adopts PEP 8
as the coding standard, where PEP
is the abbreviation of Python Enhancement Proposal
(Python Enhancement Proposal), 8 represents Is a style guide for Python code.
Let’s first look at the code in a picture
Comparing the two pieces of code in the picture above, we can find that the code they contain is exactly the same
but the right The code writing format on the side obviously looks more regular than the code segment on the left, and it will be easier and more enjoyable to read because it follows the most basic Python code writing specifications.
The following is divided into several parts to learn Python coding standards
to make our code more beautiful and beautiful!
Generally speaking, declaring encoding format is required in scripts
If The python source code file does not declare the encoding format, and the python interpreter will use ASCII encoding by default
But this has the disadvantage that once non-ASCII encoded characters appear, the python interpreter will report an error
Taking UTF-8 as an example, the following two encoding format declarations are in compliance with the rules.
# -*- coding: utf-8 -*-
# coding = utf-8
and other programming languages (such as Java, C language) use curly brackets "{}" Different from separating code blocks, Python uses code indentation and colon (:) to distinguish the level between code blocks.
In Python, for class definitions, function definitions, flow control statements, exception handling statements, etc., the colon at the end of the line and the indentation of the next line indicate the start of the next code block , and the end of the indentation indicates the end of this code block.
Note that to indent code in Python, you can use spaces or the Tab key. But whether you type spaces manually or use the Tab key, usually uses a length of 4 spaces as an indentation amount
(by default, a Tab key represents 4 spaces).
For Python indentation rules, beginners can understand it this way. Python requires that each line of code belonging to the same scope must have the same indentation amount, but what is the specific indentation amount? , there are no hard and fast rules.
Correct sample code:
a=1if a==1: print("正确") # 缩进4个空白占位else: # 与if对齐 print("错误") # 缩进4个空白占位
Wrong sample code:
a=1if a==1: print("正确") else: print("错误") print("end") # 改正只需将这行代码前面的空格删除即可
Just remember one thing: Use 4 spaces for indentation uniformly To enter, do not use tabs, and do not mix tabs and spaces
Remember this, generally speaking, indentation will not cause too big a problem!
Use # in Python to comment. When we use #, there should be a space after the
## 注释部分 # # 注释部分
When commenting inline , there should be at least two spaces in the middle
print("你好,世界") # 注释
spaces
General principles of use:
Normally, on both sides of the operator, function parameters It is recommended to use spaces to separate between and on both sides of commas.
General principles for using blank lines:
导入总应该放在文件顶部,位于模块注释和文档字符串之后,模块全局变量和常量之前。
导入应该按照从最通用到最不通用的顺序分组,分组之间空一行:
每个 import 语句只导入一个模块,尽量避免一次导入多个模块
#推荐import osimport sys #不推荐import os,sys
命名规范这一块的大家应该都比较熟悉了,但是不同的编程语言之间的明明规范也是有所区别的~
Python命名建议遵循的一般性原则:
Python中,输出语句中使用单双引号都是可以正确的,但是也有相应的编码规范
所以我们也不要随心所欲的添加引号,最好是遵循下面的规范!
引号使用的一般性原则:
Python跟其他几个主流编程语言的分号使用区别很大
Python的代码末尾不需要加分号,而Java和C#等都需要添加
不要在行尾添加分号,也不要用分号将两条命令放在同一行,例如:
# 不推荐print("Hello") ; print("World")
推荐学习:python详细教程
The above is the detailed content of Let's talk about Python's coding style. For more information, please follow other related articles on the PHP Chinese website!