LA ILAHA ILLA HU
Allah, Your Lord There Is No Deity Except Him.
Lesson 11: Comments In Python
A Comments starts with a #, and Python will ignore them.
Comments can be used to explain Python code.
Comments can be used to make the code more readable.
Comments can be used to prevent execution when testing code.
#This is a comment
print("Hello, World")
the output will be
Hello, World
Comments can be placed at the end of a line, and Python will ignore the rest of the line.
Example 1:
Code
print("Hello, World!") #This is a comment
the output will be
Hello, World!
A comment does not have to be text that explains the code, it can also be used to prevent Python
from executing code.
Example 2:
Code
#print("Hello, World!")
print("Cheers, Mate!")
the output will be
Cheers, Mate!
Multi Line Comments: Python does not really have a syntax for multi line comments.
Option 1
To add a multiline comment you could insert a # for each line
Example
Code
#This is a comment
#written in
#more than just one line
print("Hello, World!")
the output will be
Hello, World!
Option 2
Since Python will ignore string literals that are not assigned to a variable, you can add a
multiline string (triple quotes) in your code, and place your comment inside it.
Example
Code
"""
the quick
brown fox
jumps over
the lazy
dog.
"""
print("Hello, World!")
the output will be
Hello, World!
As long as the string is not assigned to a variable, Python will read the code, but then ignore it, and you have made a multiline comment.