Statements, Indentation and Comments in Python

In this tutorial, I will discuss about different statements and comments in Python. In every programming language there are different types of statements. And comments are used by programmers to increase the readability of the program.

Statements in Python

The instructions which can be executed by python interpreter are called as statements. For example, var=1 is an assignment statement. There are other statements like : if statement, for statement, while statment,etc which will be discussed in further tutorials.

Multi-Line Statements

Generally statements in Python end with a new line. However, if we wish to continue a statement in multiple lines the line continuation character ( \ ) can be used to denote that a line should continue even after newline. For example -

Total_value = value_one + \
              value_two + \
              value_three

But statements which are contained within ( ), [ ] or { } brackets can be continued in multiple lines without using line continuation ( \ ) character. For example -

Months = ["January", "February", "March", "April",
           "May", "June", "July", "August",
            "September", "October", "November", "December"]

Multiple Statements in a Single Line

In python, we can also write multiple statements in a single line using ;(semi-colon) as a separator. For example -

state = "Hello"; print(state)
Hello
a = 3; b = 9
if a < b: print("a is less than b")
a is less than b

String Python Statements

In python, a string can be declared using single or double quotes. For Example -

"Welcome to learn python"

or

'Welcome to learn python'

If we want to use quotes inside of strings in python, we neeed to make sure that we don't the same type of quotes to enclose the string. For example :

"This is a 'string'"
"This is a 'string'"

or if we want to use double quotes inside a string :

'This is a "string"'
'This is a "string"'

There is also another way to use quotes inside of a string but we will look into that when we will discuss about string data type in python.

Indentation

Like other programming languages C, C++, Java,etc Python does not use curly braces ( { } ) to define a block of statements such as class and function definitions or flow control mechanism.

Blocks in python are denoted by line indentation which is strictly followed.

The number of spaces for indentation is not defined but a block should be indented using same number of spaces not that using of tab instead of spaces for indentation is discouraged. For example -

if True :
    print("True")
else :
    print("False") 
True

Comments in Python

Comments are used to increase readability of a program. They are useful to describe the concepts of a program so that user can understand working of the program easily.
In python ( # ) symbol is used to start writing a comment. Comments are for programmers to understand the program better. Python interpreter ignores comments.

#This is a comment
print("Hello World!!") #This is a print statement
Hello World!!

MultiLine Comments

In python, comments can be written in multiple lines in two ways. One is using # in every line and another is using triple quotes ( ' ' ' ) or     ( " " " ). Triple quotes are generally used for multi-line strings but they can also be used for multi-line comments.

'''
This is a 
multi-line comment
'''
print("Hi")
# This is also
# a multi-line
# comment
Hi

In this tutorial, we have discussed about statements in python, importance of indentation in python and comments in python.
If you have any questions please comment below and also share your views and suggestions in the comment box.

Pooja is a programmer by profession and love to write articles on Python. She is highly interested in Python, Data Science and Machine Learning. Pooja holds B.E. Computer Engineering degree. She loves teaching and learning new thing.

Leave a Comment