Skip to content

Comments

In Python, comments are fundamental elements for writing clear, understandable and maintainable code. Although they don’t directly affect program execution, they play a crucial role in communication between programmers (including your future self) and in documenting the code itself.

A comment is a line or text fragment included within the source code that the Python interpreter completely ignores during execution. That is, it’s not translated into executable instructions nor consumes resources when running the program.

They are written by placing the # symbol before any text. Everything that appears after this symbol on the line is ignored by the interpreter:

Coments.py
# This is a comment that explains the following line
print('Hello PyDocs') # Print Name

Although they are not technically “comments”, you can use docstrings (strings between triple quotes) as internal documentation. These are placed inside functions, classes or modules and are used to describe their purpose:

Docstring.py
def greet():
'''
This function prints a greeting three times.
'''
    print('Hello PyDocs')
    print('Hello PyDocs')
    print('Hello PyDocs')