Commenting in Python
Single Line Comments:
"#" is used to any line python programs. Whenever Python finds a "#" in a line (except it is inside a string), the line from that character will be treated as comment. The comment ends with the line. This is as same as "//" comment in CPP.
Examples:
>>> print "Hello" # Comment1
>>> # entire line is comment here.
>>>
Multi Line Comments:
Multi line comments can be placed between """ (three single/double quotes). See example below. The lines between these set of quotes are treated as comments.
There is one more difference between single and multi line comments in Python. That is multi line comments are not encrypted in the compiled/interpreted runtime file(.pyc). So users can read them by simply opening the .pyc file. But single line coments are not readable from .pyc files. Thats why generally multi line comments are used for documentation/Help.
Take care while using multi line comments, these should always start in a new line, other wise this will be treated as strings.
Examples:
test.py
-------
""" This program demonstrates the multi line comment usage.
the code do nothing except assigning a value to a variable and printing back.
Run the code with following command
Python test.py"""
a = 10
print a
Summary:
~ Two types of commenting available in Python
~ Single Line with # or Multi Line with """ or '''
~ Multi Line comments are generally used for documentation or help because these are available even in runtime/compiled .pyc files.
~ Always start a multi line comment in a new line
No comments:
Post a Comment