Comments and indentation in python

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.


4 thoughts on “Comments and indentation in python”

  1. 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 …


Leave a Comment