Explain Use of Python Comments

By | July 18, 2019

We use Pythons Comments to make the source code easier for humans to understand. Python interpreter will ignore any comments in the program.

How to Add Python Comments in the Source Code

Python uses a Hash symbol ( # ) to indicate that rest of the line is a comment.

How to use python comments

Single Line Comments in Python

Python supports Single line comments. It uses a hash sign # to start a single comment.

Example of Single Line comments:

1 # assign 10 to n to declare and initialize a Python variable n
2 n=10
3 # check the number for even or odd 
4 if n%2==0:
5    print('Number is Even')
6 else:
7    print('Number is Odd')

Note:    Line number 1 and 3 are two single line comments in Python.

Writing Python Comment After a Statement on the Same Line

We can also use a Python comment after a statement on the same line. In this case the remaining line after # symbol is a comment now. It will be ignored by the python interpreter. For example:

n=10 # assign 10 to variable n

How to Write Multi-line Comments

Use a hash symbol ( # ) in the beginning of every line as shown in the following example Python code

 1  # this is a comment
 2  # this s second line of comments
 3  # this is third line of comments

Loading

Leave a Reply

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