Python Introduction

Python Statement, Indentation and Comments

Any instruction written in the source code and executed by the Python interpreter is called a statement. The Python language has many different types of statements like assignment statements, conditional statements, looping statements, etc., that help a programmer get the desired output. For example, p = 9; is an assignment statement. Learn python indentation here.

Python indentation

You may have found that reading and understanding the above code was a little stressful as there is no Python indentation, and all the statements are squished together. Now take a look at this code:

Python indentation

This one is easier to understand as it has proper spacing and indentation. To achieve an easy-to-understand and structured program code, we need to use the proper coding syntax. It includes using Python statements, proper indentations and comments for explanation.

Python Statement

A conditional statement is a logical expression where operators compare, evaluate, or check if the input meets the conditions and returns ‘True’. If yes, the interpreter executes a specific set of instructions. On the other hand, looping statements repeatedly execute a set of instructions as long as the defined conditions are met or satisfied.

Multiline Statements

Usually, we use the new line character (/n) to end a Python statement. However, if you want to expand your code over multiple lines, like when you want to do long calculations and can’t fit your statements into one line, you can use the continuation character (/). 


s = 10 + 15+ 30 + \
    49 + 5 + 57 + \
    3 - 54 - 2

Another way to create multiline statements is to use parentheses (), braces {}, square brackets [], or even a semi-colon (;). While the continuation character marks an obvious line continuation, the other multiline methods imply continuation indirectly.


s = (10 + 15+ 30 + 
    49 + 5 + 57 + 
    3 - 54 - 2)

p = [ 'Hello',
'to Python']

x = 5; p = 83; o = 7

We use semicolons to place multiple statements in one line.

Python Indentation

A unique feature of Python is the indentation of the block code (set of code under a loop, function, etc. Although the amount of indentation is up to you, all the statements in a block must have the same indentation. We show the end of a block by the first unindented statement. So, when you wish to exit the conditional statement, hit the Enter key to move to the succeeding line and then the Backspace to realign with the conditional statement line.


Python indentation

You can add an indent using the Tab key on the keyboard or four whitespaces. 

For example:


tep = float(input(’What is the water temperature? ’))
if tep > 24:
	print(’Water is safe!’)
	print(’Jump in!’)
	print('Better not go in!')
# First line after if-else 

Using Python indentation makes our code more readable and neat. Although indentations may be ignored in a continuous line, it is a good practice to indent. Let’s see why.

The final output will be the same for this code:


if choice == 'a': print("You chose 'a'."); a = 'yes'

and this code. 


if choice == 'a':
	print("You chose 'a'.")
	a = 'yes'

However, the second one looks more simple and is easier to understand. If you do not use proper Python indentation, the program will throw an IndentationError.

Python Comments

Although we give names to variables, define proper functions, and use a neat structure syntax, you may still find it difficult to understand a program code sometimes. You may not remember why you wrote a set of code in your program after a few weeks. Hence, adding comments is a useful method in the long run.

We use Python comments to explain what a program is doing so you can understand it easily. It also helps other users when they want to understand your code. They are an essential part of a Python code and can be docstrings or in-line explanations that explain a line of code. 

Apart from making code easy to understand, comments also act as reminders or checkpoints in a large program. They are also sometimes used to ignore certain parts of a code during testing.

We use the # symbol to start a comment. Everything written after this symbol is a comment until we add a newline character (/n). Comments are only meant for a programmer’s understanding and are ignored by the Python interpreter.

For example: 


# To take input from the user
s = int(input("Enter your number: "))

Multiline Comments

The above example shows a single line comment that gives short and precise descriptions. You can also extend the comments to more than one line by using the # symbol. You have to make sure that # is present at the beginning of each new comment line.


# this is an example
# of a multiline comment
# that extends to more 
# than one line

Another method of adding multiline comments is by using single, double or triple quotes. However, we mostly use triple quotes to add multiline comments, and as long as they are not doc strings they do not generate any extra code.


""" This is an example
of a multiline comment
with triple quotes
that extends to more 
than one line """

The difference between a multiline comment in triple quotes and a docstring is that docstrings appear immediately after we define a function, class, or module. They are associated with an object as their __doc__ attribute that we can retrieve.


def triple(numb):
    """Accepts a number numb and triples its value"""
    return 3*numb
print (triple.__doc__)



Accepts a number numb and triples its value

Questions and Answers on Python Indentation

Q1. How do I fix indentation in Python?

Answer: You can use the script in the Tools/scripts/ directory of your Python installation. You can change the Python (.py) files to make sure only 4-space indents work, and not allow any hard tab characters.  Also, make sure you trim any extra spaces and tabs from the ends of lines and remove empty lines at the end of files. You should also ensure the last line ends with a new line.

Q2. Why is indentation important in Python?

Answer: Python indentation is an essential part of a code that helps make the program more readable and understandable. Indentation is a way to tell the interpreter that a series of statements belong to a specific code block. All the statements in one block have the same indent, while the first statement after the block will have a different indent. 


p = int(input('Enter a number please: '))
while p != 0:
	for r in range (1, p):
        	print (r)

You can see how easy it is to understand the code in the above example.  

Q3. Do you have to indent in Python?

Answer: Most programming languages only use indentation to make the program code look neat. In Python, however, it is mandatory to indent statements to indicate which block of code they belong to. 

You must indent each line by the same amount to show they come under one block.


def triple(numb):
    """Accepts a number numb and triples its value"""
return 3*numb
print (triple.__doc__)

In the above example, we did not indent the return statement properly. So, the interpreter throws an indentation error.



File "", line 3
SyntaxError: 'return' outside function

Q4. How many spaces is an indent in Python?

Answer: By default, a Python indentation is equal to 4 whitespaces. However, you can use any number of spaces, but at least one space should be present to act as an indent. You can add indents by using the space key four times or the tab key one time.

Share with friends

Customize your course in 30 seconds

Which class are you in?
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
Ashhar Firdausi
IIT Roorkee
Dr. Nazma Shaik
Gaurav Tiwari
Get Started

Leave a Reply

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

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.