Python Comment Tutorial

Almost every programming language provides the comment in order to document the program, application, and script. Python also provides different comment types like single-line comment, block or multiline comments, etc. Comments can be used for different requirements and cases. The comments provide a human-friendly explanation of the specific part of the code like function, code block, variable, etc.

Comment Use Cases

Python comments can be used for different reasons and cases which are listed below.

  • Adding notes about the script, application, or program.
  • Explain the function.
  • Explaining the variable.
  • Explaining specific code blocks.
  • Explaining the code file in general.

Comment Syntax

Python use the # hash mark to create comments. Even there single, double quotes and string literals can be used comment they are not an official and reliable way. The Python hash comment sign has the following syntax where the hash mark is the start of the comment and the left side of the hash mark is interpreted as a comment and not executed as a Python statement. The right side of the hash mark is still interpreted as a Python statement.

  • # is the start of the comment.
  • COMMENT is the comment and human friently explanation of something.
  • CODE is Python code or statement which is interpereted by Python interpreter.

Single Line Comment

Lets start Python comment examples with the hash mark. The most basic creation and usage of the comment in Python is the Single Line Comment where the comment consist of single line of comment.

print("This is not comment")

#This line is a comment and not interpreter by Python

print("This is not comment")

#    This line is a comment and not interpreter by Python

Single or Double Quote as Single Line Comment

The Python provides the hash mark as default and official way of commenting. But programmers crete some alternative ways to create comments by using single or double quote. By default single or double quote are used to create string variables and set values. But if the string is not assigned into a variable it do not effects the Python code and can be used as single line comment. Both the single or double quote without variable assignment can be used as single line comment.

a = "I am a variable assignment"

"I am a double quote comment"

b = "I am a variable assignment"

"I am a single quote comment"

Block/Multiline Comments

In some cases we may need to create a lot of comment which can wrap multiple lines. This comment type is called as Block Comment or Multiline Comment . The hashmark can be used to create Block/Multiline comments. Just use multiple hash marks in neightbour lines.

print("This is not comment")

# Multiline Comment
# Multiline Comment
# Multiline Comment

String Literals as Block/Multiline Comment

The String Literals are normally used to create multi line strings by defining the string value as multiline. String literals can be also used to create Block/Multiline comments without assigning the string literal value into a string.

a = ''' This is 
        multiline string '''

''' This is 
    multiline or block
    comment '''

Inline Comments

In some cases we may need to put description or command in the same line with a python statement. Even this may seem a bit strange it is very possible to put comment after a statement. In the following example we put command after python statements like variable definition etc.

print("This is a code") # This is an inline comment

Comment In Python Code

The term comment in is used to make a python statement executable which is previously commented. As comments are not executed we can make the code executable if it is a valid Python statement. The following Python code,

# print("Hello PythonTect")

commented output by removing the hash mark like below.

print("Hello PythonTect")

Comment Out Python Code

In some cases we may need to prevent a Python statement or Python code not executed. We can make this line as comment by using the hash mark and prevent execution. Converting a python statement into a comment is called as Comment Out In the following example we comment out the Python print() statement.

print("Hello PythonTect")

Add the hash mark start of the line to comment out print() statement.

# print("Hello PythonTect")

Leave a Comment