Python – Exploring comments

Hello Friend’s

Today in this article we are going to discuss about comments in Python

What are comments in Python?

Comments in Python are the lines in the code that are ignored by the compiler during the execution of the program.

Comments are use to improve the readability of the code and help the programmers to understand the code very carefully. 

There are three types of comments –

  1. Single line comments
  2. Multiline comments
  3. Docstring comment

Comments in Python

# Python program to demonstrate comments
# sample comment

print("Knowledge junction")
------------------------------------------------
In[1] runfile (...)
Knowledge junction

In the above example, it can be seen that comments ignored by the compiler during the execution of the program.

Comments are generally used for the following purposes: 

  • Code Readability
  • Explanation of the code or Metadata of the project
  • Prevent execution of code
  • To include resources

Multiline and single lines Comments in Python

Single-Line Comments

Python single line comment starts with the hashtag symbol (#) with no white spaces and lasts till the end of the line. If the comment exceeds one line then put a hashtag on the next line and continue the comment

# print Knowledge junction
print("Knowledge junction")
---------------------------------------
In[1] runfile (...)
Knowledge junction 

Multi-Line Comments

Python does not provide the option for multiline comments. However, there are different ways through which we can write multiline comments.

""" Python program to demonstrate
 multiline comments"""
print("Knowledge junction")
-------------------------------------
Knowledge junction

We have very good series of Python articles, please have a look –

In next article – We are going to discuss about Docstrings

Thank you

Have a nice day 🙂

You may also like...

1 Response

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: