Home >Backend Development >Python Tutorial >The usage and specifications of single-line and multi-line comments in Python

The usage and specifications of single-line and multi-line comments in Python

WBOY
WBOYOriginal
2016-12-05 13:27:161574browse

Foreword

Comments can serve as notes. When working in a team, code written by individuals is often called by multiple people. In order to make it easier for others to understand the flow of the code, using comments is very effective.

Python comment symbol

1. Python single line comment symbol (#)

The pound sign (#) is often used as a single-line comment symbol. When # is used in code, any data to the right of it will be ignored and treated as a comment.

print 1 #输出1
The content to the right of the

# will not be output during execution.

2. Batch and multi-line comment symbols

In python, there will also be times when there are many lines of comments. In this case, you need to batch multi-line comments. Multi-line comments are enclosed in triple quotes ''' ''', for example:


Some people may say that this is not confused with multi-line strings, but I have seen many programmers use it this way, so just pay attention to the distinction when using it.

3. Python Chinese annotation method

When writing code in python, it is inevitable that Chinese will appear or be used. At this time, you need to add Chinese comments at the beginning of the file. If you do not declare the format for saving the encoding at the beginning, it will use ASKII code to save the file by default. At this time, if there is Chinese in your code, an error will occur, even if your Chinese is included in the comments. So it is important to add Chinese comments.

#coding=utf-8
或者:
#coding=gbk

Tips: Both of the above can represent Chinese annotations. I have seen more people using urf-8.

Summary

A good programmer must comment on the code. But make sure that the comments are all important. You can tell what it does at a glance. Useless code does not need to be commented. The above is the entire content of this article. I hope the content of this article can bring some help to everyone's study or work.

Statement:
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