PadhaiTime Logo
Padhai Time

Indentation in Python

Programming Languages like C, C++ and Java use Curly braces for a block of code as below:



if True


whereas Python uses the concept of Indentation. Indentation refers to the whitespaces (typically four spaces or a single tab) that indicate the start of a code suite (block). All statements that are indented at the same level are part of the same suite. A block of code is written inside curly brackets in various languages.


Python has simplified the syntax by removing the curly brackets from your code block. There is no need to type anything to indicate the beginning and conclusion of a block.

In Python, we utilise indentation with a colon (:) for this reason. The colon (:) marks the beginning of a new suite (block) of code that must be intended to be correct. If you forget to indent your statements after a colon, the interpreter throws an error. The end of the suite is indicated by unindenting the next line of code (which is not the part of your suite).


def foo():
    print(“Hello Padhaitime “)
    if True:
    print(“Outside If and else”)
foo()  # Call above function
print(“Outside foo function”)



Python indentation Rules:

  • A backslash cannot be used to divide indentation into numerous lines.
  • Python code's first line cannot be indented (four spaces added); otherwise, IndentationError will be raised.
  • To indent text, you shouldn't combine tabs and whitespace. It's because non-Unix text editors operate differently, and mixing them can result in incorrect indentation.
  • The tab character should not be used for indentation; instead, use whitespace.
  • Using 4 whitespaces for the initial indentation and then continuously adding 4 more to raise the indentation is the recommended technique.


Benefits of Python indentation Rules:

  • Along with Brackets, Indentation is also used in the majority of computer languages so that code looks good.
  • But in Python, indentation is a necessary step which automatically beautifies the code.
  • Python has extremely straightforward indentation rules. 
  • It is quite simple to produce properly indented code because the majority of Python IDEs do it for you automatically.


Disadvantages of Python indentation Rules:

  • Since whitespaces are needed for indentation, it can be quite time-consuming to rectify if the code is large and the indentation is incorrect.
  • Anyone coming from a different programming world may find it difficult to get used to the idea of utilising whitespace for indentation at first because the majority of popular programming languages utilise braces.



Comments in Python:

Python uses comments, which are brief explanations that are added to the code to make it easier to read. They are used by developers to record their own thought processes while they produce code. It clarifies the fundamental reasoning behind why a certain piece of code was created. They are only intended to help programmers or other developers understand a particular section of code, especially since Python comments are completely disregarded by the Python interpreter. This is demonstrated in the example that follows.



# This comment will be ignored by the interpreter
print("Welcome to Padhaitime") 


Welcome to Padhaitime


What Purposes Do Python Comments Serve?

Python comments start with the hash symbol # and continue to the end of the line. However, hash characters in a string are not considered comments. A remark can be written in three ways: as a standalone line, beside the matching code statement, or as a multi-line comment block.

Writing comments is useful in many different ways in Python. Some notable applications include:

  • Achieving more readability
  • Teaching others the code
  • After extensive practice, I can readily understand the code.
  • Including materials


What Benefits Can Python Users Expect When Using Comments?

Python comments offer a number of benefits. Their main advantages are:

  • Enables other programmers to comprehend the code with ease.
  • The code then makes sense on its own.
  • Aids in recalling the rationale for the employment of a particular command, method, or function in the code.
  • Enables the interpreter to disregard certain code portions when doing tests.


Different types of Comments in Python:

Single-line, multi-line, and docstring comments are the three different types of comments. Depending on the kind, comments have different syntaxes. Each type of remark will be covered individually in this tutorial along with examples.


1) Individual Comments:

The "#" symbol denotes the start of a single-line comment. After the hash symbol (#), anything written on a single line is regarded as a comment. The format for single-line comments is as follows:

here, # comments

In Python, there are two ways to use single-line comments. It can be used either before or after the code. The example given below demonstrates how comments can be used both ways.



 #defining general structure of a car
car = {
	"car_id":0, #car id and reference details
	"car_description":"",#general description
bike = {
	"bike_id":1, #bike id and reference details
	"bike_description":"It is a bike",#general description


{'car_id': 0, 'car_description': ''}
{'bike_id':1, 'bike_description':"It is a bike"}


2) Multi-Line Comments:

Multi-line comments are not supported by Python. There are numerous solutions to this problem, though. Although none of these approaches are technically multi-line comments, you can nevertheless use them as such. The first method involves beginning each line of the remark with a #.



# you can use
# multi-line comments
# in the way by
# including the comment character
# at the beginning of each line


3) DocString Comments:

Another way of commenting the things is docstrings. These comments are made for documentation purposes for functions etc. It involves the use of 3 quotation marks at the start and end of the comment.



Hi, This is a Doc 
String comment
which can take any no.
of lines. Just use 3 quotation marks




Bonus point:

Another way involves using string literals without putting them in any variables. The Python interpreter rejects string literals that aren't assigned to variables. Take advantage of this to write comments with many lines. You have the option of using a single (") or double ("") quotation.



# this is a comment
print("This is single-line comment ")
'this is a string literal,which is not assigned to any variable and it will be ignored by Python interpreter'


This is single-line comment 

Python's excellent commenting system makes it simple for other programmers to read and comprehend your code. It is one of several fundamental ideas in Python that you need to understand in order to understand the programming language. With the help of our Python instructional playlist for beginners, you can quickly learn all there is to know about comments and other Python concepts.

Bengaluru, India
  • We collect cookies and may share with 3rd party vendors for analytics, advertising and to enhance your experience. You can read more about our cookie policy by clicking on the 'Learn More' Button. By Clicking 'Accept', you agree to use our cookie technology.
    Our Privacy policy can be found by clicking here