Home / Python / Python Comments, Indentation and Statement

Python Comments, Indentation and Statement

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

Python Programming Language Comments

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 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 currently working line of the editor or simply before the start of the newline. 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 While Loop

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:  Python Environment Setup

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:  9 Best IDE For 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 comment or question you can contact me or ask me in the comment below.

About Jack Sparrow

Avatar
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 comes in the for seeking education. This website is the free source of learning about new technology and up to date knowledge.

Check Also

Nested-if-statement

Python Nested if Statements With Example

Python Nested if Statements With Example In this lesson, I will teach you about Python …

Leave a Reply

Your email address will not be published. Required fields are marked *