Python Comments

Like other programming language, Python also support comments.

Comments is a way of explaining Python code.

Comments is used to prevent Python code from exception.

Comments is used to make code more readability by explaining each concept or logic.

Creating Comment in Python

To create a Comment just type #symbol, then follow with the explanition.

Anything start with a #symbol, Python will treat the entire line as comment, and it will not be executed.

Example:

# this is a sample comment
print("Hello, World!")

A commentcan be place any where in your code. Example it can be placed at the end of the line, ot inside code block.

Example:

print("Hello, World!")# this is a sample comment

As I mentioned commentis not only used to explained code. But can be used to stop Python code from execution.

Example:

#print("Hello, World!")
print("Hello, Codingkoleji's!")

Multiline Comments in Python

Python is far different from other programming language when it comes to multiple comments.

Means Python doesn't have any syntax for multiple comments. You must include #symbol, in each line.

Example:

# this is a sample
# of multiple 
# line comment
print("Hello, World!")

Eventhough Python is not supporting multiple line comments.

But we can achieved that since Python is not executing string literals that not assigned to a variable.

You can used the string literals to add your comment inside.

The string literals could be add using (triple quotes) """ to your code. And guest what, you can add as multiple as you could. Then placed the comment inside.

Example:

""" this is a sample
of multiple 
line comment
"""
print("Hello, World!")

Eventhough, Python read the string literals but then will ignored it, since it not assigned to any varibale.