GeeksBro

FREE EXCLUSIVE TIPS

Python Tutorials, Tricks & Tips, Projects, etc..,

Python Comments, Statements and Indentation

In this tutorial, you will learn about Pythons' Comment, statement and the importance of indentation in Python programming language.

Python Comments

Python Comments are the instructions that the interpreter doesn't execute.

python comments

Comments are very important in the programming. They tell us, what a block of code does in the programs.

It also helps if your working in a team. So, taking time to explain the code will help you in the future. Python Comments are best for the programmers to understand the code.

SingleLine Comments

In the Python programming language, we have single line comments which start with the # character.

Program

#python code to swap 2 numbers
a = 5
b = 7
a, b = b, a
print(a, b)

Output

7 5

The line which starts with # is the comment. As you can see that the comment was ignored by the interpreter.

MultiLine Comments

In the Python Programming language, we have another type of comments called Multiline comments, which are enclosed in the triple quotes.

Program

"""this
is
multiline
comment"""
'''this
is
using
single
quotes'''
print('this is a multiline comment')

Output

this is a multiline comment

Text which is enclosed in the triple quotes are the multiline comments in Python. As you can see that the comment is ignored by the interpreter.

Multiline comments are called doc strings.

Python Statements

Instructions that the Python interpreter executes are called Statements. You can different type of statements in the programming language like loop statements, conditional statements, assignment statements, etc.,

SingleLine Statements

Examples like a = 0, b = a, if statements, for statements, etc.., are called single line statements.

>>> #single line statements
>>> a = 0
>>> b = a

All the above statements are called single line statements.

MultiLine Statements

In Python, you can extend the statements to multiline with the character \. See the example below.....

a = 2 + 5 + 4 + 5 \
    + 6 + 5

You can also use parantheses () for the multiline statements in Python. See the example below.....

a = (2 + 5 + 4 + 5 \
    + 6 + 5)

Python Indentation

Most of the programming languages like C, Java, C++, etc.., use {} for a block of code. Python uses the Indentation for a block of code.

The Indentation in Python must be consistent. Means you can use 4 spaces, 5 spaces or any as you wish but, they must be consistent throughout the block of code. End of a block can be detected when you write a line of code from the starting. See the Example below.....

Program

a = 0
if a is 1:
    print(1)
    a = 1
print(a)

Output

0

In the above example, you can see the indention in the if block. If the condition is true it will execute the 2 statements in the block otherwise it will print the a value.

If you don't use the indentation correctly you got IndetationError.

EndNote

If you have any Doubts Comment below or Message us at GeeksBro FB Page.


Post a Comment