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.
Table of Contents
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.
- Single line comments
- Inline comments
- 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 + /.