techmore.in

Python - Comments

What are Comments?

Comments in Python are used to explain code, make it more readable, or prevent execution of code while testing. Python ignores comments during execution.

1. Single-line Comments

Start with a # symbol. Everything after # on that line is ignored.

python
# This is a comment print("Hello") # This prints Hello

2. Multi-line Comments

Python doesn't have official multi-line comment syntax. You can use multiple single-line comments or a multiline string (triple quotes) not assigned to a variable.

python
"""This is a multi-line comment. It's often used to describe modules or functions.""" print("Multiline comment above.")

3. Docstrings (Documentation Strings)

Docstrings are special multi-line comments used to document modules, functions, classes, or methods. They use triple quotes and are accessible via .__doc__.

python
def greet():"""This function prints a greeting message.""" print("Hello from function!") print(greet.__doc__)