English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python Basic Tutorial

Python Flow Control

Python Functions

Python Data Types

Python File Operations

Python Objects and Classes

Python Date and Time

Advanced Python Knowledge

Python Reference Manual

Python Statements, Indentation, and Comments

In this article, you will learn about Python statements, the importance of indentation, and the use of comments in programming.

Python statements

Instructions that can be executed by the Python interpreter are called statements. For example, a = 1 is an assignment statement. If statements, for statements, while statements, etc., are other types of statements, which will be discussed later.

Multi-line statements

In Python, the end of a statement is marked by a newline character. However, we can extend a single statement to multiple lines of continuous characters (\). For example:

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

This is explicit line continuation. In Python, line continuation is implicitly implied in parentheses (),square brackets [] and curly braces {},for example, we can implement the above multi-line statements as

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

Here, the surrounding parentheses () implicitly perform line continuation. [] and {} are the same. For example:

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

We can also use semicolons to put multiple statements on a single line, as shown below

a = 1; b = 2; c = 3

Python indentation

Most programming languages (such as C, C ++,Java)all use curly braces {} to define code blocks.While Python uses indentation).

Code block (Function'sBody,Loop'sThe body, etc.) starts with indentation and ends with the first line that is not indented. The amount of indentation depends on you, but the amount of indentation must be consistent throughout the block.

Generally, four spaces are used for indentation, and they have priority over tabs. Here is an example.

The implementation of indentation in Python makes the code look neat and clean. This leads to a consistent and uniform Python program that looks similar.

Indentation can be ignored in consecutive lines. It is always a good habit to indent. It makes the code more readable. For example:

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

and

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

Both are valid and do the same thing. However, the former style is clearer.

Incorrect indentation will cause an IndentationError.

Python comments

Comments are very important when writing programs. They describe what is happening inside the program, so that the person viewing the source code will not be confused. You might forget the key details of a program you wrote a month ago. Therefore, it is always meaningful to spend time explaining these concepts in the form of comments.

In Python, we use the hash (#) to start writing comments.

It extends to the newline character. Comments are for programmers to better understand the program. Python interpreter ignores comments. 

# this is a comment
# print output Hello
print('Hello')

Multi-line comment

If we have an extended multi-line comment, one method is to use a hash (#) at the beginning of each line. For example:

# this is a long comment
# it extends
# to multi-line

Another way to do this is to use triple quotes, ''' or """.

These triple quotes are usually used for multi-line strings. However, they can also be used as multi-line comments. Unless they are not documentation strings, they will not generate any additional code.

"""This is also an
A perfect example
Multi-line comment """

Documentation strings in Python

Docstring is the abbreviation of documentation string.

It is astringas the first statement in the module, function, class, or method definition. We must write the function/The role of the class.

Use triple quotes when writing documentation strings. For example:

def double(num):
    """The function doubles the value"""
    return 2*num

Docstring as the __doc__ attribute of the function is available for us to use. After running the above program, issue the following code in the shell.                                                                                                              

def double(num):
    """The function doubles the value"""
    return 2*num
print(double.__doc__)

Output:

The function doubles the value