Tutorials

Python for Beginners: Comments in Python

Posted by I. B. Gd Pramana A. Putra, 06 Oct 22, last updated 06 Oct 22

Comments in Python are codes that the interpreter will ignore, thus the comments code will never be executed. This is intended to allow programmers to insert an opinion, note, or essential comment on a block of programming code.

Python has two types of comments: single-line comments and multi-line comments Each of them has to start with its own character which later the interpreter will recognize upon interpreting the code.

Make a single-line comment

To write a one-line comment in Python, simply put the # character at the beginning followed by the content of the comment.

# It's a single-line comment that the interpreter will ignore 
# print("hello world")

Especially in the example below you can insert a comment next to a line of program code.

print("hello world") # this comment will not be executed

print("hello world") will still get executed by the interpreter, but whatever characters remain after the # symbol will be ignored, even though they are on the same line of code. ,

Make a multi-line comment

Python also supports writing multi-line comments by using " three times on the opening and closing.

"""
This is a multi-line comment
because the comment line is more than one 
here, print("hello world") will not be executed 
"""

Another way of writing multi-line comment is by using # repeatedly on every line you prefer.

# This is a multi-line comment
# because the comment line is more than one 
# print("hello world") will not be executed 

You are free to use any commenting style that you prefer for writing a multi-line comment.

Conclusion

One day when you work on your own serious project or work in a team, you will be thankful to yourself for leaving some comments. Other programmers as well could be very grateful for the comments that you left might saved them a lot of time when they're trying to understand your codes.

Answer & Responses
    No comments yet

Wanna write a response?

You have to login before write a comment to this post.