Class 11:Python Comments

Python Comments

Comments are very important while writing a program. It describes what’s going on inside a program so that a person looking at the source code does not have a hard time figuring it out. You might forget the key details of the program you just wrote in a month’s time. So taking the time to explain these concepts in form of comments is always fruitful.

 

In Python, we use the hash (#) symbol to start writing a comment.

It extends up to the newline character. Comments are for programmers for better understanding of a program. Python Interpreter ignores comment.

#This is a comment
#print out Hello
print(‘Hello’)

#This is a comment
#print out Hello
print('Hello')
# calculate and print sum of two numbers
a=int(input("Enter 1st no "))
b=int(input("Enter 2nd no "))
c=a+b
print("Sum = ",c)

Output:

Enter 1st no 10
Enter 2nd no 20
Sum =  30
>>> 

Multi-line comments

If we have comments that extend multiple lines, one way of doing it is to use hash (#) in the beginning of each line.
For example:

#This is a long comment
#and it extends
#to multiple lines

Another way of doing this is to use triple quotes, either ”’ or “”” .

These triple quotes are generally used for multi-line strings. But they can be used as multi-line comment as well. Unless they are not docstrings, they do not generate any extra code.

”’This is also a
perfect example of
multi-line comments”’

or

“””his is also a
perfect example of
multi-line comments”””

"""
 calculate and print sum of two numbers
"""
a=int(input("Enter 1st no "))
b=int(input("Enter 2nd no "))
c=a+b
print("Sum = ",c)

Output:

Enter 1st no 10
Enter 2nd no 20
Sum =  30
>>>