Comments, Indentation and Statement In Python Programming Language

In this article, you will learn a complete and comprehensive overview related to Comments, Indentation, and Statement In Python Programming Language. You will learn why these things are important and how to use them properly.

Comments in Python Programming Language

Including comments in your program can be really useful for providing information that what are you doing in this block of code.

Suppose you handed over your piece of code to someone else for any particular purpose if you include additional supportive information in the shape of your comments it really helps him to understand your code.

Comments are ignored by the interpreter that’s why they never show up in your output window. You can put comments as a hint, or for what purpose you are writing this code and anything else that is a kind of supportive material in your code.

In Python Programming Language comments always start with “#” hash sign. The length of the comments in Python normally is to the end of the current line or before the newline starts. But in some cases, comments can be span to multiple lines.

In Python Langauge comments can be of two types which are as follows:

Python Single Line Comment

Single line comment normally spans to the current working line of the editor or simply before the start of the new line. You can use as many single-line comments as you want. There is no restriction on a number of comments you can use.

Recommended:  Python Environment Setup

You can use as many single-line comments as you want

Python Multi-Line Comments

Suppose you need to write some information related to your function but the comment goes to the next line, in this case, your single line comment cannot work here because it only works for the single current line. For this, we use Python multi-line comments.

In case your comment is long enough to go to the next line the one way to cover this comment is to add hash sign “#” in front of every new line to cover your long comment that goes to the newline like shown below

There is one other way of covering this type of long comment and that is to use triple quotes around your text. see the example below. You can use both this (‘ ‘ ‘) or (” ” “) quotes for multi-line comment.

Python Indentation

Many of the programming languages in the world like C, C++, and Java use curly braces  {  } to show the block of code. But Python programming doesn’t use curly braces to define the block of code instead it is base on indentation and strictly followed.

Recommended:  IF,ELIF,ELSE Statement in Python Programming Language

The rule is that it is totally a person’s own choice to have what number of spaces for indentation but once indent start it must remain consistent throughout the block of code otherwise you may see an IndentationError.  Generally, 4 spaces used to indent the block of code.

Below is the correct illustration of how to use indentation correctly.

As you can see the indentation remains consistent. Despite the error, the same fashion of indentation increases the readability and the code looks good.

Now observe the incorrect indentation of code which will cause an IndentationError.

The above piece of code will cause an IndentationError because it doesn’t remain consistent in his indentation.

Python Programming Language Statements

In Python Programming a statement is a complete piece of code that actually performs some kind of operation or action. Python simplifies the learning to code process that a beginner can also learn it with some effort and consistency.

In Python programming ” = ” is called assignment statement other statements are as if statement, while statement, for statement etc. Python supports some other statements but we will cover them in the upcoming lessons.

Recommended:  First Program Hello World In Python Programming Language

Multi-Line Statements in Python Programming

Python programming supports both implicit newline character or explicit newline character  ( \ ) and it is called line continuation character. Below example shows the explicit line continuation where you have to add it by your own self.

But if we write the above statement in the parenthesis (), braces {} or brackets [ ] it will add line continuation character implicitly see the example below.

Same implicit work is done if we surrounded our statement in the braces () or brackets[].

Python Programming Multiple Statement On Single Line

You can write all the long statement on a single line but remember to end each of your expression with a semicolon (;). It shows that one expression is ending here and a new expression starts. By expression, I mean any single instruction.

I hope you enjoy this lesson and if you have any comments or question you can contact me or ask me in the comment below.

Jack Sparrow

Hay I Am Admin Of This Website. I Am Very Passionate About Computers And Technology. This Website Is The Result Of My Passion. I Love To Eliminate The Barriers That Come In Seeking Education. This Website Is The Free Source Of Learning About New Technology And Up To Date Knowledge.

This Post Has 2 Comments

  1. Mike Rudloph

    nice and clean post.

    1. Jack Sparrow

      Thanks for your valuable feedback.

Leave a Reply