Comments#

In Python, comments are essential for explaining code and making it more readable. They can help others (and yourself in the future) understand what you were trying to accomplish with a certain piece of code. Python ignores comments, and they have no impact on the execution of the code.

Single Line Comments#

In Python, single line comments start with the # symbol. Anything following the # on that line is a comment.

# This is a single line comment
print("Hello, World!")  # This is a comment too, inline with code

Multi-line Comments#

Python does not have a specific syntax for multi-line comments. However, a common practice is to use the # symbol at the beginning of each line:

# This is a multi-line comment
# which spans over several lines.
print("Hello, World!")
Hello, World!

Alternatively, some developers use triple quotes (''' or """) to indicate multi-line comments. While this isn’t their intended purpose (they’re actually string literals), they can serve the function in practice if not assigned to a variable or used in an expression:

'''
This is another way to create
a multi-line comment using triple quotes.
'''
print("Hello, World!")
Hello, World!

Keep in mind that the use of triple quotes for comments is a bit controversial. It’s advisable to stick with the # symbol for consistent commenting practices.