Python comments on codes

In Python, programming comments are used to explain code, it can be used to make the code more readable. Comments are ignored by compilers or the interpreters


Objectives

  • Python Comment Syntax

  • Python Single-line comment

  • Python Inline comment

  • Python Multi-line comment

  • Python Comment for code documentation


Python Comment Syntax

Comments start with # then full line can use for write text. Comment never execute by compiler.

# This is a comment of python


Python Single-line comment on codes

# print is responsible for write/show something into console
print("This is Bismillah Program")


Python Inline comment on codes

print("This is Bismillah Program")  # We can add comment in same line of code execution


Python Multi-line comment on codes

"""
Example of Multiline comments
The sum method is responsible for addition of 2 number and return them
"""
def sum(first, second):
    return first + second


print(sum(10, 20))


Python Comment for code documentation (docstrings)

Code

def sum(first, second):
    """
    Returns the sum of two decimal numbers
    Parameters:
                    first : A decimal number
                    second : Another decimal number
    Returns:
                    sum_of_input (str): string of the sum of first and second
    """
    sum_of_input = str(first + second)
    return sum_of_input


print(sum(10, 20.50))
print(sum.__doc__)

Output

30.5

    Returns the sum of two decimal numbers
    Parameters:
                    first : A decimal number
                    second : Another decimal number
    Returns:
                    sum_of_input (str): string of the sum of first and second


Process finished with exit code 0


  • print(sum.__doc__) : Used for print the documentation


Python comment video tutorial