Home >Backend Development >Python Tutorial >How to add comments in python

How to add comments in python

藏色散人
藏色散人Original
2019-10-17 11:19:088930browse

How to add comments in python

How to add comments in python?

Any programming language is indispensable for comments, and Python is no exception. The following is the specific usage of Python comments:

Recommended: "Python Tutorial"

1. Single-line comments

Single-line comments in the Python programming language often start with #. Single-line comments can be placed as a separate line above the commented line of code, or on after a statement or expression.

Example:

# -*- coding: UTF-8 -*-
print ("hello world!");     #您好,世界

2. Multi-line comments

Multi-line comments in Python use three single quotes (''') or three Double quotation marks (""") are used to mark, but in fact this is the way to write multi-line strings, not the multi-line comment method advocated by Python itself.

Example:

'''
这是多行注释,使用单引号。
这是多行注释,使用单引号。
'''
"""这是多行注释,使用双引号。这是多行注释,使用双引号。"""

3. Coding comments

When developing Python, a coding statement is required. If UTF-8 encoding is used, it must be made above the source code# -*- coding: UTF-8 -*- Statement, starting from Python3, Python uses UTF-8 encoding by default, so the source files of Python3.x do not need to specially declare UTF-8 encoding.

4. Platform comments

If you need to make the Python program run on the Windows platform, you need to add the #!/usr/bin/python comment above the Python file.

In addition to serving as documentation, Python comments You can also debug the code, comment out part of the code, and troubleshoot the remaining code to find the problem and improve the code!

The above is the detailed content of How to add comments in python. For more information, please follow other related articles on the PHP Chinese website!

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