How to Comment in Python Code?

Posted in /  

How to Comment in Python Code?
vinaykhatri

Vinay Khatri
Last updated on March 28, 2024

    When we code in Python, or any other programming language, it becomes important to write code that is easily understandable by other developers. Not just that, but reading your code and making sense out of it when you revisit it after a long time might become troublesome, especially if the code is of huge size.

    That's why learning how to add a comment to Python code is important. Giving variables a logical name and making your code more modular by defining Python functions will make your code more readable, but by writing Python comments, you can increase the readability of your Python code.

    By the end of this Python tutorial article, you will have a complete idea of writing comments in Python.

    You will also learn why is it important to add comments to code? What are the different types of comments in Python? And how to write Python comments to make the code cleaner and legible.

    What are Comments in Python?

    Python comments are that text in the code that is not executed by the interpreter. The Python interpreter ignores the comment lines and moves further in the Python code. The sole purpose of the comments is to provide extra information about the code snippet. Thus, comments have nothing to do with the program execution and program result. The Python program will give the same result with or without comments unless you comment out some code lines. To add a comment in Python, we use the # symbol.

    Example

    #this is a comment in Python.
    
    

    The above example represents a single-line comment. You can also use multi-line comments to comment out multiple lines. You will learn how to add single and multi-line comments in the upcoming sections of this article.

    Why is it Important to Write Comments in Your Code?

    Programmers and coders use comments in their programs to explain the working of modules and functions as a docstring. Comments play an important role in writing good code. The proper use of comments helps the developer to grab a brief idea about the working of a module or code snippet. Suppose you worked on a project six months ago, and now your boss wants you to add some updates on that project.

    By now, you don't have a good idea about the functioning of every module because the project contains thousands of modules and functions. Only if you have mentioned the proper comments in the program will you be able to understand the overall working of each and every module. Else, you have to go through the complete project to find out the working of the program.

    Writing comments in the program is a good practice in programming, and it is one of the things that make your program look good.

    A well-coded program makes it easier for developers to understand what the program is about and how it is going to work.

    It is not necessary that good code always gives you a perfect output, but it surely helps you to debug the code easily. If you write code for a big project, then other developers who will be working on the same project will also read your code.

    So, by mentioning comments for every module you write, collaboration becomes easier.

    How to Write Comments in Python?

    Now we know what comments are in Python and why it is important to write comments in a program . Next, let’s discuss how to write comments in Python. As discussed before, there are two ways of how can we add a comment in Python:

    1. Single line comments using #
    2. Multi-line comments using the triple single or double quote “”” or ‘’’

    1) Python Single Line Comments

    To add a single line comment in a Python program, we use the hash (#) symbol. All the text that is written in a line after the # symbol is treated as a comment by the Python interpreter and gets ignored.

    Example 1

    # this is a comment in Python.

    Example 2

    # this add() function adds two numbers.
    def add(a, b):
            return a+b
    
    print(add(3, 4))  # calling the add() function and printing the result.

    In the above Python program, you can see that only the text that comes after the # symbol is treated as a comment.

    2) Python Multi-line Comments

    If you want to write a long description of the module, function, or code snippet that needs more than one line, then you can use a multi-line comment. However, you can also use the # symbol to comment on multiple lines. For every line, you need to add the # symbol. Thus, using a multi-line comment is a better approach. To write a multi-line comment in Python, we can either wrap the comment with triple single or double quote marks.

    Example 1

    '''
    this is a comment
    which is wrapped with
    triple single quotation
    '''

    OR

    """
    
    this is a comment
    which is wrapped with
    triple double quotation
    """

    Example 2

    '''
    this add() function adds two
    numbers and returns the sum.
    '''
    def add(a, b):
            return a+b
    
    print(add(3, 4))

    Conclusion

    In this tutorial, we learned how to comment in Python code and why it is important. Comments are generally used to write alternative information about the code, but they can also come useful in debugging. Thus, many programmers use comments to debug their programs.

    By knowing how to write a comment in Python, you can make your code more readable not only for your future use but for others too who will apparently read your Python code.

    People are also reading:

    Leave a Comment on this Post

    0 Comments