Python Comments

comments in python

Python Comments is an essential tool for the programmers.Python Comments are generally used to explain the code. We can easily understand the code if it has a proper explanation. A good programmer must use the comments because in the future anyone wants to modify the code as well as implement the new module; then, it can be done easily.

In the other programming language such as C++, It provides the // for single-lined comment and /*…. */ for multiple-lined comment, but Python provides the single-lined Python comment. To apply the comment in the code we use the hash(#) at the beginning of the statement or code.

Example:

# This is the print statement  

print(“Hello Python”)  


Multiline Python Comment

We must use the hash(#) at the beginning of every line of code to apply the multiline Python comment.

Example:

# First line of the comment   
# Second line of the comment  
# Third line of the comment  

Example:

# Variable a holds value 5  
# Variable b holds value 10  
# Variable c holds sum of a and b  
# Print the result  
a = 15 
b = 10  
c = a+b  
print("The sum is:", c)  

Output:

The sum is: 25

The above code is very readable even the absolute beginners can under that what is happening in each line of the code. This is the advantage of using comments in code.

We can also use the triple quotes (”””) for multiline comment. The triple quotes are also used to string formatting.


Docstrings Python Comments

The docstrings comment is mostly used in the module, function, class or method. It is a documentation Python string. We will explain the class/method in further tutorials.

Example:

def intro():  
  """ 
  This function prints Hello Joseph! 
  """  
  print("Hi Joseph!")              
intro()  

Output:

Hello Joseph!

Recommended Posts:

Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *