Python Comments, Statements, Indentation & Docstring

    In this tutorial, we will discuss some important concepts of Python, which are very important and most used to describe the procedure of Python code.

    so let's discuss Python's Statements Indentation, comment, and Docstring.

    Python Statement

    In Python, usually, you will come to hear the word “Python Statement”. What does it mean? In simple words, any instruction that can be executed by the Python interpreter whether it gives an error or not, is known as a Python statement.

    For example, var=40 is a statement where we are assigning the value 40 to a variable var . Similar there are many other statements such as if statement, while statement, and many more which we will discuss later.

    Python Indentation

    In other high-level programming languages when we write a block of code, which belongs to a single statement, we use braces to hold that code.

    In Python, we do not use braces, we use indentation. For indentation, you can use white space or just press the tab button.

    Note: There are 4 white spaces in a standared indentation.

    Let’s understand it with an example:

    #syntax of a for loop using indentation 
    for i in range(1):
        print("this is a part of For statement")
        print("we have use indentation before printing this statement")
    
    print("THIS IS NOT A PART OF FOR STATEMENT we haven’t used indentation here")

    # OUTPUT

    this is a part of For statement
    we have use indentation before printing this statement
    THIS IS NOT A PART OF FOR STATEMENT we haven’t used indentation here

    Python Comments (#)

    In Python, there is a concept known as Python comments , which are the simple text statement starts with # that Python does not treat as code and does not execute.

    The Python interpreter just ignores the comment and moves to the next line of code. In Python, whether it is a code or a normal text, you can make it a comment by using a special symbol hash (#).

    The question is, why do we need something which does not execute? In every programming language, you will find this concept of comment, and they are designed for a purpose, i.e., suppose if a third person checks your source code, how will he/she supposed to know what a specific line or function or class is going to do. There we use comments and add some text information. So that the third person can understand why we have declared or written this code.

    Let’s take an e.g.

    # this is a comment 
    # and the Python interpreter is not going to execute it
    
    print(" Hello World") 
    #This statement will print Hello World and this is a comment
    
    #print("Hello World 2")
    #Python interpreter will not execute this statement because we #have used (#) before the print() statement so it’s a comment now

    #Output

    Hello World

    Multiline Comments

    In the above example, we have seen that the hash (#) comment can only comment a single line of code but what If we want to comment multiple lines. For that, we use triple quotes single and double quotation.

    Let’s understand it with an example.

    # this is a single line comment using hash (#)
    
    # to make this line a comment we again used hash (#) at the beginning of this line
    
    """this is a multi-line Comment
    
    print("Hello") 
    This code won't be executed because it is inside the multi-line comment this line is the last line of the multi-line comment because we have used """

    Python Docstring

    Docstring stands for documentation string, it looks like a multi-line comment but has a different purpose. A docstring is used inside the function, and class body to give some additional information.

    To use a docstring, either  double """ or single '''  triple quotes. The only difference between a docstring and a comment is you can call the docstring text by using the special keyword __doc__ .

    Let’s understand it with an example.

    def print_hello():
        '''this is a doc string of function print_hello, which is used to print hello'''
        print("hello")
        print(print_hello.__doc__)
    
    print_hello()
    

    # Output

    hello
    this is a doc string of function print_hello, which is used to print hello

    Conclusion

    Everything in Python the code we write with syntax is known as the Python statement.

    Comments are the special statement that starts with # symbol or quoted inside the triple """ double and single ''' quotes. Comments in python are generally used to provide specific messages and debug the code.

    Python indentation is used to write the block code, for statements like function, for, if, while, class, etc.

    A doc string is a special multiline comment inside the function and class body that provides additional information about the function and class.