Curriculum
In this tutorial, you will learn about comments in python and how to create different types of comments in Python.
Topics Covered:
Commenting on a set of codes makes it more understandable and readable to other users. When you create a complex code in python, it is important to have comments in between the code to help you and other programmers to understand your program and the logic in which the program is being written.
For instance:
a=57 b=75 c=(a+b(a*b)) #the value of c is given by the total product given by the multiplication ‘a’ and ‘b’ and ‘a+b’ print (c)
In the above program, the logic is very simple. The value of C depends on the values of ‘a’ and ‘b’ as the value of ‘c’ is the total product given by the multiplication ‘a’ and ‘b’ and ‘a+b’. You may understand the code at the first instance here, but imagine looking at a code consisting of 500 lines. You may not understand the logic of the program at the first glance. So that’s why it is important to add comments in between the code to detail the logic of the program in every instance.
Now let us look at the types of comments available in Python
The single-line comments are short comments that are commonly used to provide short explanations for variables, function declarations, and expressions.
It starts with # at the beginning of the line and lasts till the end of the physical line.
Example:
print("Welcome to Developer publish Academy and you’re learning Python programming") #The above code prints this statement: Welcome to Developer publish Academy and you’re learning Python programming.
Similar to single-line comments, Multiline comments in python use # for commenting but here we use multiple #s for multiple lines. If you want to add a comment of 4-5 lines then begin every line with the #.
Example:
#Multline comments #The above code prints this statement: Welcome to Developer publish Academy and you’re learning about multiline comments print(“Welcome to Developer publish Academy and you’re learning about multiline comments”)
You would have learnt about the use of a string literal in printing a multiline statement in the previous lessons. We are going to apply the same concept here, add (‘“) before and after a comment. You can even type a whole paragraph and comment on it just by adding (‘“) in the beginning and ending of the paragraph.
Example:
'''we are going to see about docstring comments in this program''' print(“Docstring Comments in Python”)