Python Comments

When number of lines in your programs get increased and it get more complicated, then it is very difficult to read. Formal languages are dense, and it is often difficult to look at a piece of code and figure out what it is doing and why. For this reason, it is always a best idea to add certain notes to your programs to explain in natural language what your program is doing. These notes are called comments, and in Python they start with the # symbol. Comments are most useful when they document non-obvious features of the code. It is reasonable to assume that the reader can figure out what the code does; it is much more useful to explain why.

x = 10 # assign 10 to x
print("Value of x is:",x) #print value of x

Output:

>> >
Value of x is: 10

Multi-line comments:

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

#Here is an example of
#multi-lines comments
#in python.

Other way of doing the same multi-line comments just putting triple quotes, either ''' or """.

"""Here is an example of
multi-lines comments
in python."""

OR

'''Here is an example of
multi-lines comments
in python.'''

Docstring in Python:

Python documentation strings (or docstrings) provide a convenient way of associating documentation with Python modules, functions, classes, and methods. An object’s docsting is defined by including a string constant as the first statement in the object’s definition. For example, the following function defines a docstring:

def my_fun():
    """Take two numbers as input 
    and print the sum of the two numbers.
    """
    a=10
    b=20
    c=a+b
    print(c)
print(my_fun.__doc__)
my_fun()
Try it Yourself

Declaration of docstrings:

The following Python file shows the declaration of docstrings within a python source file:

"""
Assuming this is file called test.py, then this string is
first statement in the file. This will become the "test" module's
docstring when the file is imported.
"""
class TestClass(object):
    """The test class's docstring"""

    def test_method(self):
        """The test method's docstring"""

def test_function():
    """The test function's docstring"""

import test
help(test)
help(test.TestClass)
help(test.TestClass.test_method)
help(test.test_function)

Output:

>>> import test
>>> help(test)
Help on module test:

NAME
test

DESCRIPTION
Assuming this is file called test.py, then this string is
first statement in the file. This will become the “test” module’s
docstring when the file is imported.

>>> help(test.TestClass.test_method)
Help on class TestClass in module test:

>>> help(test.test_function)
Help on function test_method in module test:

Take input from User

Conditional Execution