Skip to main content

Introduction to Python Comments

Python Comments

Comments in Python are used to add explanatory notes or documentation within the code.

They are ignored by the Python interpreter and do not affect the execution of the program.

Comments are helpful for making the code more understandable and providing context to other developers or yourself when revisiting the code in the future.

Python supports two types of comments:

Single-line comments

Single-line comments are used for adding comments on a single line. They start with the # symbol and extend until the end of the line. Anything written after the # symbol on the same line is considered a comment.

# This is a single-line comment
x = 5 # Assigning a value to variable x

Multi-line comments

Multi-line comments, also known as docstrings, are used for adding comments that span multiple lines.

They are often used to provide detailed explanations or documentation for functions, classes, or modules.

Multi-line comments are enclosed between triple quotes (''' or """) and can extend over multiple lines.

'''
This is a multi-line comment.
It can span multiple lines.
'''

"""
This is another multi-line comment.
It can also span multiple lines.
"""