Open In App

Statement, Indentation and Comment in Python

Last Updated : 10 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Here, we will discuss Statements in Python, Indentation in Python, and Comments in Python. We will also discuss different rules and examples for Python Statement, Python Indentation, Python Comment, and the Difference Between ‘Docstrings’ and ‘Multi-line Comments.

What is Statement in Python

A Python statement is an instruction that the Python interpreter can execute. There are different types of statements in Python language as Assignment statements, Conditional statements, Looping statements, etc. The token character NEWLINE is used to end a statement in Python. It signifies that each line of a Python script contains a statement. These all help the user to get the required output.

Types of statements in Python?

The different types of Python statements are listed below:

Example: 

Statement in Python can be extended to one or more lines using parentheses (), braces {}, square brackets [], semi-colon (;), and continuation character slash (\). When the programmer needs to do long calculations and cannot fit his statements into one line, one can make use of these characters. 

Declared using Continuation Character (\):
s = 1 + 2 + 3 + \
    4 + 5 + 6 + \
    7 + 8 + 9

Declared using parentheses () :
n = (1 * 2 * 3 + 7 + 8 + 9)

Declared using square brackets [] :
footballer = ['MESSI',
          'NEYMAR',
          'SUAREZ']

Declared using braces {} :
x = {1 + 2 + 3 + 4 + 5 + 6 +
     7 + 8 + 9}

Declared using semicolons(;) :
flag = 2; ropes = 3; pole = 4

What is Indentation in Python

Whitespace is used for indentation in Python. Unlike many other programming languages which only serve to make the code easier to read, Python indentation is mandatory. One can understand it better by looking at an example of indentation in Python.

Role of Indentation in Python

A block is a combination of all these statements. Block can be regarded as the grouping of statements for a specific purpose. Most programming languages like C, C++, and Java use braces { } to define a block of code for indentation. One of the distinctive roles of Python is its use of indentation to highlight the blocks of code. All statements with the same distance to the right belong to the same block of code. If a block has to be more deeply nested, it is simply indented further to the right.  

Example 1:

The lines print(‘Logging on to geeksforgeeks…’) and print(‘retype the URL.’) are two separate code blocks. The two blocks of code in our example if-statement are both indented four spaces. The final print(‘All set!’) is not indented, so it does not belong to the else-block. 

Python3




# Python indentation
 
site = 'gfg'
 
if site == 'gfg':
    print('Logging on to geeksforgeeks...')
else:
    print('retype the URL.')
print('All set !')


Output

Logging on to geeksforgeeks...
All set !

Example 2:

To indicate a block of code in Python, you must indent each line of the block by the same whitespace. The two lines of code in the while loop are both indented four spaces. It is required for indicating what block of code a statement belongs to. For example, j=1 and while(j<=5): is not indented, and so it is not within the while block. So, Python code structures by indentation.

Python3




j = 1
while(j <= 5):
    print(j)
    j = j + 1


Output

1
2
3
4
5

What are Comments in Python

Python comments start with the hash symbol # and continue to the end of the line. Comments in Python are useful information that the developers provide to make the reader understand the source code. It explains the logic or a part of it used in the code. Comments in Python are usually helpful to someone maintaining or enhancing your code when you are no longer around to answer questions about it. These are often cited as useful programming convention that does not take part in the output of the program but improves the readability of the whole program. 

Comments in Python are identified with a hash symbol, #, and extend to the end of the line.

Types of comments in Python

A comment can be written on a single line, next to the corresponding line of code, or in a block of multiple lines. Here, we will try to understand examples of comment in Python one by one:

Single-line comment in Python

Python single-line comment starts with a hash symbol (#) with no white spaces and lasts till the end of the line. If the comment exceeds one line then put a hashtag on the next line and continue the comment. Python’s single-line comments are proved useful for supplying short explanations for variables, function declarations, and expressions. See the following code snippet demonstrating single line comment:

Example 1: 

Python allows comments at the start of lines, and Python will ignore the whole line.

Python3




# This is a comment
# Print “GeeksforGeeks” to console
print("GeeksforGeeks")


Output

GeeksforGeeks

Example 2: 

Python also allows comments at the end of lines, ignoring the previous text.

Python3




a, b = 1, 3  # Declaring two integers
sum = a + # adding two integers
print(sum# displaying the output


Output

4

Multiline comment in Python 

Use a hash (#) for each extra line to create a multiline comment. In fact, Python multiline comments are not supported by Python’s syntax. Additionally, we can use Python multi-line comments by using multiline strings. It is a piece of text enclosed in a delimiter (“””) on each end of the comment. Again there should be no white space between delimiter (“””). They are useful when the comment text does not fit into one line; therefore need to span across lines. Python Multi-line comments or paragraphs serve as documentation for others reading your code. See the following code snippet demonstrating a multi-line comment:

Example 1:

In this example, we are using an extra # for each extra line to create a Python multiline comment.

Python3




# This is a comment
# This is second comment
# Print “GeeksforGeeks” to console
print("GeeksforGeeks")


Output

GeeksforGeeks

Example 2: 

In this example, we are using three double quotes (“) at the start and end of the string without any space to create a Python multiline comment.

Python3




"""
This would be a multiline comment in Python that
spans several lines and describes geeksforgeeks.
A Computer Science portal for geeks. It contains
well written, well thought
and well-explained computer science
and programming articles,
quizzes and more.
"""
print("GeeksForGeeks")


Output

GeeksForGeeks

Example 3:

In this example, we are using three single quotes (‘) at the start and end of the string without any space to create a Python multiline comment.

Python3




'''This article on geeksforgeeks gives you a
perfect example of
multi-line comments'''
 
print("GeeksForGeeks")


Output

GeeksForGeeks

Docstring in Python

Python Docstrings are a type of comment that is used to show how the program works. Docstrings in Python are surrounded by Triple Quotes in Python (“”” “””). Docstrings are also neglected by the interpreter.

Python3




# program illustrates the use of docstrings
 
def helloWorld():
  # This is a docstring comment
    """ This program prints out hello world """ 
    print("Hello World")
 
 
helloWorld()


Output

Hello World

Difference Between ‘Docstrings’ and ‘Multi-line Comments 

Docstrings and Multi-line comments may look the same but they aren’t.

  • Docstrings are written in the functions and classes to show how to use the program.
  • Multi-line comments are used to show how a block of code works.


Previous Article
Next Article

Similar Reads

Python TabError: Inconsistent Use of Tabs and Spaces in Indentation
Python, known for its readability and simplicity, enforces strict indentation rules to structure code. However, encountering a TabError can be frustrating, especially when the code appears to be properly aligned. In this article, we'll explore what a TabError is, and how to resolve TabError in Python. What is TabError in Python?A TabError is a type
2 min read
Python Indentation Error: Inconsistent use of Tabs and Spaces
In Python programming, making sure you use the same kind of spacing when you indent your code is important. If you mix tabs and spaces in your indentation, it can cause errors in your code. This article will explain what this error is, why it happens, and offer solutions to fix it. What is Inconsistent Use Of Tabs And Spaces In Indentation Error?Th
3 min read
Indentation in Python
Indentation is a very important concept of Python because without properly indenting the Python code, you will end up seeing IndentationError and the code will not get compiled. Python Indentation Python indentation refers to adding white space before a statement to a particular block of code. In another word, all the statements with the same space
3 min read
Indentation Error in Python
In this article, we will explore the Indentation Error in Python. In programming, we often encounter errors. Indentation Error is one of the most common errors in Python. It can make our code difficult to understand, and difficult to debug. Python is often called a beautiful language in the programming world because we are restricted to code in a f
3 min read
Proper Indentation for Multiline Strings in Python
In Python, indentation plays a crucial role in code readability and structure, especially with multiline strings. Multiline strings, defined using triple quotes (""" or '''), allow for strings that span multiple lines, preserving the formatting within the quotes. Proper indentation of these strings is important for maintaining code readability and
3 min read
Python PRAW – Getting the comment karma of a redditor
In Reddit, a redditor is the term given to a user. Reddit karma is the score you get for posting and commenting on Reddit. Here we will see how to fetch the karma obtained by commenting on Reddit by a redditor. We will be using the comment_karma attribute of the Redditor class to fetch the comment karma. Example 1 : Consider the following redditor
2 min read
Python PRAW - Getting the ID of a comment in Reddit
In Reddit, we can post a comment to any submission, we can also comment on a comment to create a thread of comments. Reddit assigns each and every comment with an identification. Here we will see how to fetch the ID of a comment using PRAW. We will be using the id attribute of the Comment class to fetch the ID of a comment. Example 1 : Consider the
2 min read
Python PRAW - Getting the body of a comment in Reddit
In Reddit, we can post a comment to any submission, we can also comment on a comment to create a thread of comments. Every comment have some textual information in it which is called the body of the comment. Here we will see how to fetch the body of a comment using PRAW. We will be using the body attribute of the Comment class to fetch the body of
2 min read
Python PRAW - Checking whether a comment has been edited or not in Reddit
In Reddit, we can post a comment to any submission, we can also comment on a comment to create a thread of comments. A comment can be later edited after posting it. Here we will see how to check whether a comment has been edited or not using PRAW. We will be using the edited attribute of the Comment class to check whether a comment has been edited
2 min read
Python PRAW - Getting the time when a comment was posted on Reddit
In Reddit, we can post a comment to any submission, we can also comment on a comment to create a thread of comments. Here we will see how to fetch the exact time when a comment was posted using PRAW. We will be using the created_utc attribute of the Comment class to fetch the Unix time when the comment was posted. Example 1 : Consider the following
2 min read
Article Tags :
Practice Tags :