How to add comments in python?

Comments in Python are the short and precise description of the code block. They are the notes in a programing language. Comments are a handy feature of programing languages especially when the size of our program grows and become more complicated. You can write the comments (notes) that can describe your overall approach to solve the problem. Moreover, it also helps in the readability of code. While working in a team, comments are useful as it provides an ease to the team members to understand your code and also help you to understand it and reuse it. A comment allows you to write notes in your language most preferable in English.


comments in python, python multiline comment, python comment block, comment multiple lines python, python comment syntax, python,

How to write a comment in Python?

In python programming, everything that comes after a hash (#) symbol is considered as a comment. (#) sign indicates the start of a comment. Let’s take the following example:

 

>>> # statement to print

>>> Print (“Hello python coders!”)

 

Output:

Hello python coders!

 

 Comments does not have to be a phrase to explain the code, it can also be used to prevent Python from executing code:

 

>>> # Print (“Hello coders!”)

>>> Print (“Hello python coders!”)

 

Output:

Hello python coders!

 

 You can also add a multiline comment in the following two ways:

Using a hash (#) symbol at the start of every line:

 

>>> # This is an example of a multiline comment

>>> # Print (“Hello coders!”)

>>> Print (“Hello python coders!”)

 

Output:

Hello python coders!

 


Using string literals:

 

>>> """

>>>This is an 

>>> example of 

.>>> multiline comment

>>> """

>>> Print (“Hello python coders!”)

 

Output:

Hello python coders!

 



String literals that are not assigned to a variable get ignored by the python interpreter. So, you can add the comments in the triple quotes (""").

What kind of comment you should write?

While writing the comment considers the following point:

  • Clarity: Your comments should be clear enough that whenever you or your teammates read that they can easily tell the working of code.
  • Concise: Your comments should be as much concise as possible. Don’t add the story or paragraphs describing your code.
  • Meaningful: Comments should be meaningful to the reader.

Comments are much essential in professional career because most software is developed by a team and they also view your code and understand it to add their own code. Try to add meaningful comments and it will only be done through practice so, start practising it right away.

That’s all about comments in python.

If you found this article helpful share it with others. Let me know your queries and suggestions in the comment section below. Do subscribe my site and channel as I’m going to upload a series of articles and videos on programming in python and I don’t want you to miss any update.

Moreover, you can also contact me and share your queries on Instagram, Facebook or Twitter. I would love to provide a solution to your issues.

Stay tuned and strive to learn. Keep learning, keep growing!

 

 

 

Post a Comment

0 Comments