Statements & Comments in Python | Writing Single & Multiline Comments, Statements

After Python Indentation, the next important concepts to know before you start coding are statements and comments in Python. So here is an article on how to write single and multiline comments, statements in Python.

Python Statements

Instructions given to the Python interpreter by us are called statements. Every line of code you write is an individual statement. There are different types of statements like Assignment statements (a=5), Conditional/Compound statements (for, while, if else) and Multiline statements.

You will learn about Assignment & Conditional statements later on, but as of now, let’s just understand what are multiline statements.

Multiline Statements in Python

Multiline statements are those statements which get extended to more than one line but they still need to be read by an interpreter as one single statement. In such cases, we use parentheses (), curly braces {}, square brackets [] or slash (\) to indicate this. Also, several statements can be written in one single line using a semicolon; at the end of each statement.

For example,

Using slash \
a = 100 + 99 + 98 + 97 + \
96 + 95 + 94 + 93 + \
92 + 91 + 90

Using round brackets ( )
a =  ( 100 + 99 + 98 + 97 +
96 + 95 + 94 + 93 +
92 + 91 + 90 )

Using square brackets [ ]
a =  [ 'Learn', 'Python', 'on',
'FACEPrep']

Using curly brackets { }
a =  { 100 + 99 + 98 + 97 +
96 + 95 + 94 + 93 +
92 + 91 + 90 }

Using semicolon ; we can write multiple statements in one line.
a = 100; b = 99; c = 98;

Python Comments Syntax

Comments are lines written in English to enhance the readability and understanding of the code/logic to a third person. These are called comments as they are written to help a human understand the code and not the compiler. The compiler generally ignores all the comments.

In languages like C, C++, Java, we indicate single-line comments using // and multiline comments using /* & */. Where as comments in Python are written as discussed below.

a) Single-Line Comments

In Python, single-line comments are indicated using a hash # symbol. They are useful for explaining something short, like a variable, a function declaration etc. Here is an example of how you can use them.

#This is a Python Program
#We are trying to print a string
a = "FACE Prep"   #We are assigning the word FACE Prep to the variable a
print(a)   #We are printing the value or characters stored in the variable a
#This is the end of the Python Program

Output: As you can see, the comments get ignored by the compiler and hence they do not get printed or throw an error.

comments in python examples

b) Multi-Line Comments

Multiline comments in Python are used when you want to declare an entire paragraph or a series of statements as comments. For this, you need to use the delimiter (” or ‘) symbol thrice at the beginning and end of the comments. An example is,

"""
This is a Python Program
We are trying to print a string
"""
a = "FACE Prep"
print(a)
#This is the end of the Python Program

Shortcuts to Writing Comments in Python

It gets difficult to manage comments when you are code gets really long and complex, so in such cases, you can use the below shortcut commenting techniques.

  • To declare a single statement as a comment, place your cursor anywhere in that statement and press cltr + / and immediately # gets added at the beginning.
  • To declare several lines of code as comments in Python, select all those line/statements and then press cltr + / on Windows and Command + / on MAC.

Comments in Python FAQs

a) Single Line Comments
b) Multi Line Comments

Single line comments in Python begin with # symbol and Multiline comments in Python begin with “”” or ”’ and end with “”” or ””.