How to block comment in python?

Unlike many other languages, Python does not have any syntax to write a comment. However, you can use # and triple quotes symbols for the multiline comment.

Introduction to comments in Python

While writing a program, you may often need to explain the code. For example, you want to describe some code that works or the functionality of a specific function. Sometimes, you want to define functions parameters, algorithms, and business logic of your source code. You do these tasks by creating a comment along with the snippet.

At the time of execution, the Python interpreter ignores the commented text and only executes the code.

Another use of Python comments is that it makes an unwanted part invisible to the interpreter, and the part is not executed.  

Types of Python Comments

Python provides three types of comments:

  • block comment
  • inline comment
  • document string or docstring comment

Block comment

Single line Block comment is placed before a code which it explains. Generally, it has the same level of code it defines.

You can write a block comment with a ‘#’ and a space followed by a single line string.

Example:

# concatenates two strings

def conCat(fstring,sstring):

        print(fstring+ “ “ +sstring)

Inline comment

Python Inline comment is similar to block comment except that it is placed with the code it defines.

You write inline comments in the same line of your code. For this, write a ‘#’ and a space followed by some singleline text.

Example:

num = num * num # calculates square of a number

Document string or docstring comment

Docstring is a string literal in Python which you can use to comment. Document string uses “”” before and after to create triple quoted strings. Triple quotes are a multiline string that spans a string over many lines.

Example

“””

This is the main program

The function takes two numeric inputs

The function calculates a powered b

“””

def Power(a, b):

        print(a*b)

Unlike ‘#’ comment, docstring takes memory on your computer. 

The docstring is used for Python documentation, and technically it is not a comment. 

Multiline comments in Python

Python does not allow you to place multiline comments like other programming languages. You can create a docstring if you want many lines not to be part of the actual code. However, triple quotes take memory and are not technically comments. So, you may want another simple way for the job.

We have discussed two singleline comments such as block and inline. You can use them for writing multipleline comments. All you need to do is write # and space before all those lines you want the interpreter to ignore.

Example:

# this is the main program

# the function takes two numeric inputs

# the function calculates a powered b

def Power(a, b):

        print(a*b)

Conclusion

Comments make the code more readable and understandable. They also provide ease in understanding the business logic and functionality of the source code. Many languages such as Java, C++ provide a syntax for comment. Python has syntax for singleline comments. However, it does not have a special mechanism for multiline comments. You can write multiline comments by either using # or by docstring.

In the article, we discussed three types of Python comments block, inline and docstring. You can use block and inline commenting for a single line that uses “#” and space.

1 thought on “How to block comment in python?

Leave a Reply