in ,

Python Statement, Indentation, and Comments

Python Statement, Indentation and Comments

Python Statement, Indentation, and Comments:

In this tutorial, you will learn about Python statements, why indentation is important, and the use of comments in programming.

In this article, you will learn-

Python Statement

A statement in Python is a logical instruction that a Python interpreter can read and execute. In Python, it could be an expression or an assignment statement.

The assignment statement is basic to Python. It characterizes the manner in which an expression creates objects and preserves them.

Instructions that a Python interpreter can execute are called proclamations. For instance, a = 1 is an assignment statement. if statement, for the statement, while statement, etc. are other kinds of statements which will be discussed later.

Multi-line Statement

In Python, the finish of an announcement is set apart by a newline character. Be that as it may, we can make a statement extend over multiple lines with the line continuation character (\). For instance:

a = 1 + 2 + 3 + \
    4 + 5 + 6 + \
    7 + 8 + 9

This is an explicit line continuation. In Python, line continuation is implied inside parentheses ( ), brackets [ ], and braces { }. For instance, we can implement the above multi-line statement as:

a = (1 + 2 + 3 +
    4 + 5 + 6 +
    7 + 8 + 9)

Here, the surrounding parentheses ( ) do the line continuation implicitly. Same is

the case with [ ] and { }. For example:

colors = ['red',

We can also put multiple statements in a single line using semicolons, as follows:

a = 1; b = 2; c = 3

Python Indentation

Most of the programming languages like C, C++, and Java use braces { } to characterize a square of code. Python, however, uses indentation.

A code block (body of a function, loop, etc.) begins with space and finishes with the first unindented line. The measure of space is up to you, yet it must be predictable all through that block.

Generally, four whitespaces are used for indentation and are favored over tabs. Here is an example.

for i in range(1,11):
    if i == 5:

The authorization of space in Python makes the code look flawless and clean. This outcome in Python programs that appear to be comparable and predictable.

Indentation can be ignored in line continuation, however, it’s consistently a smart thought to indent. It makes the code progressively clear. For instance:

if True:
    a = 5


if True: print('Hello'); a = 5

both are valid and do the same thing, but the former style is clearer.

Incorrect indentation will result in IndentationError.

Python Comments

Comments are significant while writing a program. They depict what is happening inside a program, with the goal that an individual taking a gander at the source code doesn’t have a hard time figuring it out.

You might forget the key details of the program you just wrote in a month’s time. So setting aside the effort to clarify these ideas as remarks are consistently productive.

In Python, we use the hash (#) symbol to begin writing a comment.

It extends out up to the newline character. Remarks are for developers to more readily comprehend a program. Python Interpreter ignores comments.

#This is a comment
#print out Hello

Multi-line comments

We can have comments that extend up to multiple lines. One way is to use the hash(#) symbol at the beginning of each line. For example:

#This is a long comment
#and it extends
#to multiple lines

Another way of doing this is to use triple quotes, either ”’ or “””.

These triple quotes are generally used for multi-line strings. Be that as it may,

they can be used as a multi-line remark too. Except if they are not docstrings,

they don’t create any extra

"""This is also a
perfect example of
multi-line comments"""

Docstrings in Python

A docstring is short for documentation string.

Python docstrings (documentation strings) are the string literals that show up directly after the meaning of a capacity, technique, class, or module.

Triple statements are used while writing docstrings. For instance:

def double(num):
    """Function to double the value"""
    return 2*num

Docstrings show up directly after the meaning of a function, class, or module. This isolates docstrings from multiline remarks using triple quotes.

The docstrings are related to the object as their __doc__ attribute.

So, we can access the docstrings of the above function with the following lines of code:

def double(num):
    """Function to double the value"""
    return 2*num


Function to double the value

Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

Python Keywords and Identifiers

Python Keywords and Identifiers

Python Variables, Constants, and Literals

Python Variables, Constants, and Literals