Looping Constructs in Python: A Guide to for and while Loops

Python Statements, Indentation, and Comments

Python Statements

Statements are the executing instructions that are written in the source code. The Python programming language supports a variety of statement types, including assignment statements, conditional statements, looping statements, etc. All of these assist the user in obtaining the desired outcome.

An assignment statement would be something like a = 1. Other types of statements that will be covered later include the if statement, for statement, while statement, etc.

Multi-Line Python statements

Python allows for the extension of statements to one or more lines using parentheses (), braces, square brackets [], semicolons ();), and continuation characters (). One can utilize these letters when a programmer wants to do lengthy computations and cannot fit all of his statements on a single line.

In Python, a newline character signifies the conclusion of a sentence. The line continuation character (), however, allows us to have a sentence span many lines. For instance:

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

There is a clear line continuation here. In Python, the use of parentheses (), brackets (), and braces () imply the continuation of a line. For instance, we may put the multi-line sentence above into practice as:

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

Here, the line continuation is handled automatically by the parenthesis enclosing it (). The same is true for [] and []. For instance:

colors = ['red',
          'blue',
          'green']

Using semicolons, we can also group several statements on a single line as seen below:

a = 1; b = 2; c = 3

Python Indentation (Python statements)

Any combination of the statements is known as a block. The grouping of statements into a block might be thought of as serving a specific function.

Braces are commonly used in programming languages like C, C++, and Java to define code blocks. Python uses indentation to draw attention to the code blocks, which is one of its distinguishing characteristics. Python uses whitespace to indicate indentation. 

The same block of code contains all statements that are spaced to the right by the same amount. A block is simply indented farther to the right if it needs to be more deeply nested.

An indented line marks the beginning of a code block (the body of a function, a loop, etc.), which ends with the first unindented line. You decide how much indentation to use, but it must be uniform across that block.

Indentation is typically done with four whitespaces, rather than tabs. Here’s an illustration.

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

Python enforces indentation, which gives the code a tidy, clean appearance. This leads to Python scripts that have a unified appearance.

It is always a good idea to indent, however it can be disregarded in line continuation. The code is easier to read as a result. For instance:

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

And

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

Both are legitimate and accomplish the same purpose, but the first approach is more understandable.

Incorrect indentation will result in IndentationError.

Python Comments(Python statements)

Python programmers frequently utilize the comment system because, without it, things may quickly become quite perplexing. The developers’ helpful information is provided in the comments, which helps the reader comprehend the source code.

The logic, or a portion of it, employed in the code is explained. When you are no longer around to address queries about your code, comments are typically useful to someone maintaining or improving it.

These are frequently mentioned as helpful programming convention that does not affect the program’s output but enhances the readability of the entire program. In Python, there are two categories of comments:

When developing a program, comments are crucial. They explain what occurs within a program so that a person reading the source code does not struggle to understand it.

In a month, you could forget the essential components of the software you just built. Therefore, taking the effort to clarify these ideas in comments is usually beneficial.

Singel-Line Comment(Python statements)

The hash (#) symbol is used in Python to write a  single-line comment.

Up to the newline character, it is present. Comments help programmers comprehend a program better. Comments are ignored by the Python interpreter.

If the comment spans more than one line, add a hashtag to the next line before continuing. Short explanations of variables, function declarations, and expressions can be provided using Python’s single-line comments. See the following example of a one-line remark in code:

#This is a comment
#print out Hello
print('Hello')

Multi-line Comments

Up to numerous lines can be used for comments. One method is to start each line with the hash(#) symbol. For instance:

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

Alternatively, you might use triple quotes in the form of ”’ or “””.

Multi-line strings often employ these triple quotes. However, they can also be utilized as multi-line comments. They don’t produce any extra code unless they are docstrings, in which case they do.

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

Docstring

Docstrings: A sort of remark called a docstring is used to explain how a program operates. Docstrings have triple quotes around them (“”” “””). The interpreter also doesn’t pay attention to docstrings.

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

Output

Hello World

The following are the differences between “Docstrings” and “Multi-line Comments”:

Note: that despite their similarities, docstrings and multi-line comments are different.
  • In order to explain how to use the software, docstrings are written in the functions and classes.
  • Coding blocks are explained in multi-line comments.

Leave a Reply