Python Comments 1.7

In this tutorial, we will learn about Comments in Python and we will also cover different examples related to Python Comments. And, we will cover these topics.

  • Python Comments
  • Multiline Python Comment
  • Docstrings Python Comment
  • Python indentation

Python Comments

In python or in any other code we generally use the comments to make proper understanding for a code. In the professional world or programmers, every programmer uses the comments in code which will help to prepare them to modify the code if it is required in the future we can use that and make sure to get the proper understanding of the code.

We can see in another programming language such as c or C++ they use the comment statement like // is used for the single-line comment and to comment multiples lines we use /*…. */ in python we use the # to comment on a line and it is used at the beginning of the code.

Example:

# This is the print statement  
print("Welcome To PythonTpoint")  

Output:

Python Comment Example
Python Comment Example

In this code, we use the print statement and with help of #, we add a comment inside this code.

Multiline Python Comment

In Multiline Python Comment we have to use the # at the beginning of the line that will help to make a multiline code here we can understand this with the following example:

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

Code Example:

# Variable p holds value 5  
# Variable y holds value 10  
# Variable t holds sum of p and y  
# Print the result  
p = 8 
y = 23  
t = p+y  
print("The sum is:", t)  

Output:

Python Multiline Comment Example
Python Multiline Comment Example

In the above code we can see with the help of comments we can understand what is happening in the code as it is very easy to understand and readable for a viewer about this code. We also use the (“””) to add multiline comments. The triple quotes are also used to make a string format.

Docstrings Python Comment

In Docstrings Python Comment it is mostly used in the module, function, class, or method. It is known as Document String Python Comment we can consider the following example:

Example:

def introduce():  
  """ 
  This function prints Hi PythonTpoint
  """  
  print("Hi PythonTpoint")              
introduce()  
Document String Python Comment Example
Document String Python Comment Example

Python indentation

In Python Indentation it is used to define the block of the code. As in other programming languages like java, c, c++ they use the curly braces {} and in python, indentation is used whitespaces are used as an indentation in python.

When we apply indentation we generally keep in mind that we need to use the four white spaces as indentation. It depends upon the user but it must be consistent throughout that block.

Example Code:

py = int(input("Enter the number:"))  
if(py%2 == 0):  
    print("Even Number")  
else:  
    print("Odd Number")  
     
print("Tata Bye Bye !!") 

Output:

Python Indentation Example
Python Indentation Example

So, in this tutorial, we discussed Python Comments and we have also covered different examples related to its implementation. Here is the list of examples that we have covered.

  • Python Comments
  • Multiline Python Comment
  • Docstrings Python Comment
  • Python indentation

Do Follow:

Python Variables

Python Datatypes

1 thought on “Python Comments 1.7”

  1. I have recently started a site, the information you offer on this site has helped me greatly. Thank you for all of your time & work. “The achievements of an organization are the results of the combined effort of each individual.” by Vince Lombardi.

    Reply

Leave a Comment