Python Comments

When writing code, we often want to describe what the code does, explain complex logic, or make notes for ourselves or others. Comments are the perfect tool for this. They are ignored by the Python interpreter when the program is executed, allowing us to annotate our code without affecting its functionality. Python supports three types of comments:

1. Single-line comments

2. Multi-line comments

3. Documentation strings (docstrings)

Advantages of Using Comments

Using comments in your code has several benefits:

Improved Readability: Comments help others (and your future self) understand what your code is doing and why.

Code Execution Control: You can use comments to temporarily disable code for testing or debugging purposes.

Project Overview: Comments can provide metadata about the project, such as the author, date, or purpose.

Resource Addition: Comments can include links to resources or documentation relevant to the code.

Types of Comments in Python

1. Single-Line Comments

Single-line comments are used for short explanations or notes about your code. They start with a hashtag (#) and continue until the end of the line.

Example:

# This is a single-line comment
print('Hello Python!!')

Output:

Hello Python!!

In the example above, the comment # This is a single-line comment is ignored by the Python interpreter. You can also add comments at the end of a line of code:

print('Hello Python!!') # This is a single-line comment

The output will be the same as before, and the comment will be ignored.

2. Multi-Line Comments

Python does not have a specific syntax for multi-line comments, but there are ways to achieve this.

Using Multiple Hashtags (#)

You can use multiple hashtags to create a series of single-line comments.

Example:

# This is a comment
# that spans multiple lines
# using multiple hashtags

Each line with a # will be treated as a comment and ignored by the interpreter.

Using String Literals

You can also use string literals for multi-line comments. Python ignores string literals that are not assigned to a variable, so you can use them as comments.

Example:

'''
This is a comment
that spans multiple lines
using a string literal
'''

When you run this code, it won't produce any output since the string literal is ignored.

3. Python Docstring

Docstrings are a special type of comment used to document modules, classes, and functions. They are written using triple quotes (""" or ''') and are placed just below the function, class, or module definition.

Example:

def add(a, b):
"""
This function adds two numbers and returns the result.
:param a: First number
:param b: Second number
:return: Sum of a and b
"""
return a + b

print(add.__doc__) # Accessing the docstring

Output:

This function adds two numbers and returns the result.
:param a: First number
:param b: Second number
:return: Sum of a and b

In this example, the docstring provides a detailed explanation of the add function, including its parameters and return value. Docstrings can be accessed using the __doc__ attribute, making them an essential tool for documenting code in Python.