How to add comments in python code:

Comments are basically non-executable lines added in your program. While actual execution of program these commented lines are ignored by interpreter. Every programming language has its own way of inserting non-executable lines in the code. This provision of inserting comments in program is highly used in daily programming work. You can write anything as a comment in your program. Comments in python programming can be used to write a short description about the code. so that next time if any other person tries to review the code, it would be easily understood just by reading the comments.

In python there are two types of comments:

  1. Single line comment: This is used to comment only single line.
  2. Multi-line comment: Using this you can comment multiple lines in one go. You can comment big chunk of code or large description of code.

Let’s see how to insert the comments in python program.

Single Line comment in python:

In python single line comment starts with Hash “#” sign. After the “#” sign anything written till the end of physical line is considered as a comment and it gets ignored by the python interpreter.

In the example below you can see that whatever written after “#” sign is completely ignored.

  • “This is first comment” is an examle of inserting comment before code.
  • “This is second comment” is an example of in-line comment.
  • The last commented line is actual print function but it is ignored by the interpreter since the code line is starting with “#” sign.

How to insert single line comment in python

Multi-line comment in python:

In python multi-line comment starts with triple single or double quotes ”’. Content between starting triple single/double quotes and ending triple single/double quotes will be considered as commented section. That will be completely ignored by the interpreter.

In the example below we have commented three lines using multi-line comment. See that after the execution of actual program only print functions are executed.

mutliline comments in python

Indentation in python:

In programming languages like java, c++ code is grouped using opening and closing parenthesis “{ }”.

This grouping of the code in python is achieved using indentation. Grouping is required to group the function/loop code.

python tutorial on how to add indentaion in python code General indentation is four white spaces or a single tab. But again it completely depends on individual how much indentation he wants to keep. Please see the example below.

So Guys this is all about comments and indentation in python. if you want to know about how to install python and pycharm then please read earlier articles.

 

Share this Post

7 Comments

  1. Your blog is very good. Please add advanced concepts

    1. Thanks Sujith for the appreciation.. Definitely I’ll try to add advanced concepts as well..

  2. You actually make it appear so easy along with your presentation but
    I in finding this topic to be actually something that I believe I would by
    no means understand. It kind of feels too complicated and very huge for me.
    I am looking forward on your subsequent put up, I’ll try to get the hold of
    it!

  3. Tips. elif is short for else if, and you can use as many elif-blocks as needed.; As usual, each of the code blocks in an if/elif-statement must be consistently indented the same amount. Not only does Python require this indentation so that it can recognize the code blocks, but the consistent indentation makes it easy for people to recognize the if/elif-conditions and their corresponding code …

  4. Hi there! Such a good write-up, thank you!

    1. Thanks for the appreciation..

  5. Could you inform me what style are you using on your site?
    It looks nice.

Leave a Comment

Your email address will not be published.