Comments: These are the statements that are ignored by the python interpreter and not executed at all.
Why Comments are used:
1) To explain the program
2) To make the program more understandable
3) To prevent the statements from execution for testing purpose.
Types of Comments in Python:
1) Single Line Comment
2) Multi LIne Comment
Single Line Comment: The statements written after # symbol are treated as single line comments in python. These statements are ignored and not executed by python. We can write single line comment in interactive mode as well as script mode as:
>>>#this is an example of single line comment >>>print("Gargs Academy") Gargs Academy
To explain the part of the python code, We can also write single line comments as:
>>> print("Gargs Academy")#this is also an example of single line comment Gargs Academy
Multiline Comments: The comments spanning across multiple lines are called as multiline comments. Multiline comments are enclosed in triple quotes ”’ and ”’ or “”” and “””. You can write multiline comments in the python program in any one of the following two ways (using single ”’ and ”’ or double “”” and “”” triple quotes) as
"""This is an example of multiline comments explained by gargs academy sonipat 9467863365""" print("Free Online Coaching") print("Relevant Material")
After ignoring the comments, output will be:
Free Online Coaching Relevant Material
If you write multiline comments in python shell, it will be treated as multiline string as.
>>> """This is an example of multiline comments explained by gargs academy sonipat 9467863365""" 'This is an example of multiline comments\nexplained by\ngargs academy\nsonipat\n9467863365' >>> '''This is an example of multiline comments explained by gargs academy sonipat 9467863365''' 'This is an example of multiline comments\nexplained by\ngargs academy\nsonipat\n9467863365'
If we write multiline comments as part of a line, then python will report an error as:
>>> a=5 """This is an example of multiline comments explained by gargs academy sonipat 9467863365""" SyntaxError: invalid syntax >>> a=5 '''This is an example of multiline comments explained by gargs academy sonipat 9467863365''' SyntaxError: invalid syntax