Posted at: https://laptrinhcanban.com/en
Instructions on how to comment in python. You will learn how to comment in python with pound sign #
, how to comment multiple lines in python as well as how to apply comment in python after this lesson.
What is comment in python
Comments in python are lines of code that are ignored when running a python program, to help you save necessary information such as the date and time of writing the program, the name of the writer, the writing goal, explain sub-items in python program etc.
Necessary information is kept by comment in python making it easier for you to maintain the program, as well as easily transfer the project to others or share the project with many people.
Comment in python
To write comments in python, we write the following mark #
with the following syntax:
#comment
In which comment
is the comment content which placed after the mark #
.
Comments in python are calculated from behind the trailing mark #
to the end of the line. We can comment to the beginning or the middle or anywhere in the statement, just like in the following example:
## |
Note that we can not comment in python if in that statement the line break and write on multiple lines like below example:
num = 1 + 2 + 3 + 4 \ # Cannot comment on this location. |
The error SyntaxError
will appear as follows:
File "Main.py" , line 1 |
- On how to line break in a statement and write it on multiple lines, you can refer to the article Line break in python.
Comment multiple lines in python
Tocomment multiple lines in python, we write comment lines between three single quotes '''
or three quotation marks """
with the following syntax:
"""
Comment line 1
Comment line 2
…"""
'''
Comment line 1
Comment line 2
…'''
The essence of this writing is that we declare a multi-line string in python and do not add any handling commands to the newly declared string.
Therefore, the newly created multi-line chain will only store information without affecting the result of program processing.
- On how to declare a multi-line string in python , you can refer to the declaration in python in detail
We use this quotation mark to comment over multiple lines in python like following example:
""" |
You can see how this comment is used a lot in the documentation on Gihub, or in AI programming programs.
Note, when using this method, all comment lines must have the same indentation. If there is only one line with a different indent than the rest, an error will occur like the following example:
for i in range ( 3 ): |
Error returned:
File "Main.py", line 8 |
The reason is that compared to the two lines of print above print("comment in python")
and below print(i)
, the indentation of the comment lines is not the same.
We need to correct the indentation like this:
for i in range(3): |
For loop results will run smoothly as follows:
comment in python |
Application of comment in python
Use comments in python to hold information
When writing a python program that is too long, or a project with lots of python programs combined, you should use comments in python to store essential information such as the date and time of the program written, the name of the writer, writing goals, explaining sub-entries in python programs and so on.
This is useful when a program crashes and you need to go back somewhere in the program to fix it, and you can’t remember what that line of code means. Then, if you have commentd the meaning of that statement, then remembering and editing the code will be a lot easier.
Especially in large projects where many people participate in writing programs, comment will help your teammates understand each part of the program better.
Assuming someone is new to the project, you will not take the effort to explain it back to them, read the caption yourself and tinkering :)).
Use comments in python to ignore statements
In addition to using comment in python to hold essential information, you can also use statement ignore comment in python. This approach is called コ メ ン ト ア ウ ト in Japanese.
The way to use comment to ignore the statement in python is like the following example:
price = 500 |
If we run the above program, we will have the result:
150.0 |
If we don’t want to run the second command line anymore, we can delete it. However if we just temporarily do not want to run this command line, comment the second command line and ignore them while processing the program as follows:
price = 500 |
By turning the second line of code into a comment in python, we omitted that command line while processing the program. So only lines 1 and 3 are processed and the result is returned as follows:
100 |
Summary
Above, Kiyoshi showed you how to comment in python . To better understand the lesson content, practice rewriting today’s examples.
And let’s learn more about Python in the next lessons.
URL Link
HOME › python for beginners>>basic knowledge of python programming