Image

Anyone Can Code.





Image


Comments



Sammarth Kumar



Comments are, as the name suggests, comments on Python code. So what is the use of having comments? Well, here you go:

  1. Comments can provide valuable metadata(data about data), for example the author and license
  2. Comments provide valuable documentation and explain how the code works. This can help others understand your code and even will be useful if you return to the code after a while
  3. Comments can be used to keep the text of the code in your workspace, but make the interpreter ignore it. This becomes very useful when you are debugging your code
  4. Comments can be used to separate sections of code in a program

Now, let's get into how to make text a comment. There are mainly two ways we can create comments:

1. Single Line Comment

Single line comments can be created by using a hash # before the line you want set as a comment.


    #this is a comment
    print("this is not a comment")
                                          
this is not a comment

Note that the # will only comment out the line that it's used for. As you can see, the second line still runs.

Single line comments can also be used next to regular code.


    print("this is code") #this is a comment
                                          
this is code

Use single line comments to explain a particular line or give a header to a section of code

2. Multi line Comments

Multi line comments are used to comment out multiple lines of code or text. It is helpful for creating docstrings (to be discussed later), and also when a single line comment starts overflowing. It's particularly useful to comment out a large portion of text.


    """
    This is 
    a 
    multi line
    comment
    """
    print("Follow Q-Programming on Instagram!")
                                          
Follow Q-Programming on Instagram! (Check us out!)

To create a multi line comment, enclose the text within a pair of triple quotes. They can either be single '''comment''' or double """comment""". However, you cannot do this: "'"comment"'".

Remember, in order to be a good programmer, you need to be able to explain your code to others. That can be achieved with comments. Make it a habit to write comments while you program. This practice will be particularly helpful when you are writing large programs and need to separate your code into sections. It will also help you understand a piece of coded if you decide to leave it and go back to it later.


That's it for this tutorial! Click Next to access the next tutorial!

❮ Back

Next ❯









Copyright © 2020, Q-Programming | Created by Arjun Sodhi, Balpreet Juneja and Sammarth Kumar | Developed and Designed by Sammarth Kumar