Python Comments, Statements & Indentation – An Exclusive Guide

For programmers who have been coding in languages like C and Java, Python’s syntax will turn out to be the easiest to learn and comprehend.

Whereas if you are completely new in the “programming circles” and you have decided to start your journey with Python, will find coding really effortless and simple.

Without any further delay, let’s go ahead and learn the most fundamental things about Python’s syntaxPython Statements, the need for indentation and working with comments.

Python Comments, Statements & Indentation

Python Comments

Comments provide a way to leave an explanation or annotation in your source code. Comments are programmer-readable and are added to make the source code easier for a programmer to understand.

There might come instances when you have to write a really long program and you might end up forgetting why you wrote a particular piece of code earlier in your program. Also, while working in a team, another programmer should be able to read and comprehend your source code.

That’s where comments prove to be useful.

When the Python interpreter comes across a comment, it ignores the comment and moves to the next line of code.

Comments in Python are of following types:

1. Single line comments

The start of a single-line comment in Python is specified using the hash(#) character.

Example:

# this is a comment

2. Multi-line comments

You can have a comment that spans multiple lines by enclosing it within triple quotes (”’ or “””).

Example:

'''This is a multi-line comment
   which is extended across 
   multiple lines'''

3. Docstrings

In Python, docstring refers to documentation string. A docstring is included as a first-line inside a function, module, class or method. Docstring is a short description of what your function, module or class does.

We write docstrings inside triple quotes.

Example:

def function():
    """ The function prints hello world """
    print(“Hello, World!”)

Python Statements

A statement specifies an instruction given to the computer to perform some task. Statements are read by the interpreter which then converts them into machine-readable instructions. These instructions can now be understood and executed by the machine.

Therefore, Statements form the smallest executable unit within a Python program.

Python supports the following kinds of statements:

1. Empty Statements in Python

The simplest yet complete statement in Python is the empty statement, that is, the pass statement.

When the interpreter encounters a pass statement, it does nothing and moves on. Now, what exactly is the use of a statement that does nothing?

You’ll come across certain instances where you’ll need a statement syntactically but your program’s logic will not require one. So to avoid any potential syntax errors, we use the pass statement.

You can use it within a python loop, function or class which is not implemented yet but you want to implement it in the future.

Example :

def function():
    pass

class myclass:
    pass

for i in range(9):
    pass

2. Simple Statements in Python

Any single executable statement in Python is a simple statement.

Example:

name = input(“Enter name:”)

value = 10 + 20 + 30 + 40

Simple statements are usually single-line statements. And, the end of the line marks the end of a statement in Python. But in order to make our program more readable, we can extend a simple statement into multiple lines. Such statements are called multi-line statements.

We can extend a statement across multiple lines using the line continuation character, that is, the backslash( \ ) character.

Example for Multi-line statements:

value = 10 + 20 + 30 + \
     40 + 50 + \
     60 + 70

In python, line continuation is implied automatically inside parentheses (), [], {}.

The next example will result in the same output as the previous example.

value = (10 + 20 +
        30 + 40 +
        50 + 60 + 70)

Similarly, we can implement a long list in multiple lines as:

mylist = [ 10, 20, 30,
         40, 50, 60]

Python also allows us to write multiple statements into a single line by separating each pair of statements using a semicolon(;) as:

name = ‘Techvidvan’ ; value = 10 ; print(name, value)

3. Compound Statements in Python

A compound statement basically is a group of statements executed as a unit. It  is made up of:

  • a header line ( followed by a colon : ), and
  • a body containing a sequence of statements at the same level of indentation.

Example:

if a > b:
    greater = a
    print(“a greater than b”)
  • if a > b: is the header line
  • The two statements indented below the header line form the body of the compound statement or the suite.

We have mentioned the term “indentation” a few times now.

Well what does “indentation” mean and why is it so important in Python?

Python Indentation

Indentation refers to the whitespaces (usually 4 spaces or a single tab) that signify the beginning of a suite (block) of code. All statements indented at the same level belong to the same suite.

In other languages, a block of code is written inside curly braces ( { and } ).

Python has made the syntax a bit simpler by removing these curly braces from the block of your code. You do not need to type anything to mark the beginning and the end of a block.

Instead in Python, we use indentation along with colon ( : ) for that purpose. The colon ( : ) introduces a new suite(block) of code that must be intended to right. The interpreter raises an error if you forgot to indent your statements after a colon. The end of the suite is specified by unindenting the next line of code(which is not the part of your suite).

Example:

if x > y:
    print("x is greater than y")
elif y > x:
    print("y greater than x")
else:
    print("x is equal to y")

Summary

Here we come to the end of our tutorial.

In this article, we learned about the smallest executable unit in Python, that is, Python Statements. We also learned how Python uses indentation to mark the beginning and end of a block of code and how indentation makes your code look cleaner.

And lastly, we learned how you can use comments in python to make your program self-explanatory to a person reading it.