0% found this document useful (0 votes)
11 views5 pages

PY_CHAPTER_2_TOPIC_2

ALLADI CLOUD TRAINING

Uploaded by

actnowcloud
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views5 pages

PY_CHAPTER_2_TOPIC_2

ALLADI CLOUD TRAINING

Uploaded by

actnowcloud
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

COMMENTS IN PYTHON

Comments are non-executable lines in code that are used to describe the
functionality, clarify logic, or add notes for developers. They are ignored by the
Python interpreter when the program is run.

There are two main types of comments in Python:

1. Single-line comments
2. Multi-line comments

1. Single-Line Comments

In Python, single-line comments start with a hash symbol #. Everything after the #
on that line is considered a comment and is ignored by the Python interpreter.

Syntax:

# This is alladi cloud training institute

Example:

# This line is a comment and will not be executed


print("Hello, World!") # This is another single-line
comment

In this example, only the print() statement will be executed, and everything
after the # is ignored.
2. Multi-Line Comments

While Python does not have a specific syntax for multi-line comments like other
languages (such as /* ... */ in C or Java), you can achieve multi-line
comments in two main ways:

Option 1: Using Multiple Single-Line Comments

You can write multiple single-line comments by placing # at the beginning of each
line.

# This is a comment
# that spans
# multiple lines
print("This is a valid multi-line comment")
Option 2: Using Triple Quotation Marks (''' or """)

Another common way to write multi-line comments is to use triple quotes.


Although this is technically a string literal (often used for docstrings), it can act as
a multi-line comment when it's not assigned to a variable or used in the code.

'''
This is a multi-line comment.
It can span multiple lines
and is useful for large blocks of comments.
'''
print("Triple quotes can be used as multi-line
comments")

or

"""
This is another way to create
a multi-line comment in Python
using triple double quotes.
"""
print("Using triple double quotes")

Note: While this method is widely used for comments, technically it is a string that
isn't assigned or used in the code. Python ignores such unassigned strings, which
makes them act like comments.

3. Commenting Best Practices

 Use comments to explain why, not how: Code should be self-explanatory


regarding "how" it works. Use comments to explain the "why" behind
complex logic.
 Keep comments concise and clear: Avoid long and unnecessary comments.
A good comment should be brief but informative.
 Keep your code clean: Only comment on code that is not obvious to
someone reading it.
 Update comments regularly: Ensure comments stay relevant. Outdated
comments can confuse future developers.

Example of Good Commenting:

# Calculate the area of a rectangle


width = 5
height = 10
area = width * height # area = width multiplied by
height
print(f"The area is {area}")
Example of Over-Commenting:
# Assign 5 to width
width = 5
# Assign 10 to height
height = 10
# Multiply width by height to get area
area = width * height
# Print the area
print(f"The area is {area}")

In this example, the comments are unnecessary because the code is simple and
self-explanatory.

4. Inline Comments

An inline comment is a comment that is written on the same line as the code. These
should be used sparingly and only when necessary to clarify specific code.

Syntax:

statement # Inline comment


Example:

x = 10 # Assign 10 to variable x

Best Practice: Leave at least two spaces before the # symbol in inline comments
for better readability.
5. Using Docstrings for Documentation

Though not exactly comments, Python supports docstrings, which are multi-line
strings used to document modules, classes, functions, and methods. Docstrings are
written inside triple quotes """ or '''.

Example:

def greet():
"""
This function prints a greeting message.
It doesn't take any arguments or return any value.
"""
print("Hello, World!")

Docstrings are special because they can be accessed programmatically, allowing


tools like pydoc to generate documentation from them.

Conclusion

 Use single-line comments for brief explanations and notes.


 For multi-line comments, either use multiple # or triple quotes.
 Write comments that explain the intent or logic behind the code.
 Keep comments relevant, clear, and concise.
 Consider using docstrings for documenting functions and classes.

You might also like