Python Keywords

Python Comments

In Python, comments are used to make the code more readable and to explain the purpose of certain code blocks. Comments are ignored by the Python interpreter and do not affect the execution of the program.

Types of Comments in Python

1. Single-line Comment

A single-line comment starts with the # symbol.

# This is a single-line comment
print("Hello, World!")  # This comment explains the print statement
        

2. Multi-line Comment

Python does not have a specific syntax for multi-line comments, but we can use triple quotes (''' or """) as a workaround.

"""
This is a multi-line comment.
It is often used for documentation purposes.
"""
print("Multi-line comments in Python!")
        

3. Docstrings (Documentation Strings)

Docstrings are used to describe a function, class, or module. They are written inside triple quotes and can be accessed using the __doc__ attribute.

def greet():
    """This function prints a greeting message."""
    print("Hello, Python!")

print(greet.__doc__)  # Accessing the docstring
        

Conclusion

  • Use # for single-line comments.
  • Use triple quotes (''' or """) for multi-line comments.
  • Use docstrings (""") for documenting functions, classes, or modules.