You are currently viewing How to write Comments in Python (with examples)

How to write Comments in Python (with examples)

In programming, comments are used to explain the code in human language. It is a best practice to put clear comments in your code. Comments assist the programmers to understand the logic behind any piece of code.

What are the comments in python?

In programming, comments are used to explain the code in human language. It is a best practice to put clear comments in your code. Comments assist the programmers to understand the logic behind any piece of code.

Types of comments in python

There are three types of comments in the python language.

  1. Single line comments
  2. Inline comments
  3. Multiline comments

How to write comments in python

Single line comment 

# This is an example of single line comment

Inline comment

print(“Hello World”) # This is an example of an inline comment

Multiline comment

# This is the first line comment

# This is the second line of comment

# This is the third line of comment

Python interpreter ignores the strings which are not assigned to the variables. So, we can use string literal to comment multiple lines in python. The commenting of the several lines or blocks can be done using quotations “”” or ‘’’ apostrophes in python language.

“”” 

This is the first line comment

This is the second line of comment

This is the third line of comment

“””

Keyboard shortcut to comment lines in python

You have to select the relevant lines and press the keys Ctrl + / to comment on single or multiple selected lines. You can uncomment these lines by using the same keys Ctrl + /.

Leave a Reply