Comment in python | Laptrinhcanban.com

HOME › >>

Comment in python

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:

## 
# initialization: 2021-01-08
# author: Kiyoshi
##

age = 30 # creates the age variable

#in screen
print (age)

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. 
5 + 6
print (num)

The error SyntaxError will appear as follows:

File "Main.py" , line 1 
num = 1 + 2 + 3 + 4 \ # Cannot comment on this location.
^
SyntaxError: unexpected character after line continuation character
  • 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

"""

Hoặc

'''
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.

We use this quotation mark to comment over multiple lines in python like following example:

""" 
Date of writing: 210121
Author: Kiyoshi
"""

print ( "comment in python" )

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 ): 
print ( "comment in python" )
""""
An error will occur if commenting here.
"""
print (i)

Error returned:

  File "Main.py", line 8
print(i)
^
IndentationError: unexpected indent

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):
print("comment in python")
"""
The error did not occur because the lines have the same indent
"""
print(i)

For loop results will run smoothly as follows:

comment in python 
0
comment in python
1
comment in python
2

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
price = price * 1.08
print (price)

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 
#price = price * 1.08
print (price)

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

https://laptrinhcanban.com/en/python/nhap-mon-lap-trinh-python/kien-thuc-can-ban-ve-chuong-trinh-python/chu-thich-trong-python/

Thank you for reading and please Like & Share for other friends to learn.
">

HOME  › >>

  • Recent Posts
Profile
きよしです!笑

Author: Kiyoshi (Chis Thanh)

Kiyoshi was a former international student in Japan. After graduating from Toyama University in 2017, Kiyoshi is currently working at BrSE in Tokyo, Japan. Kiyoshi là một cựu du học sinh tại Nhật Bản. Sau khi tốt nghiệp đại học Toyama năm 2017, Kiyoshi hiện đang làm BrSE tại Tokyo, Nhật Bản.