Python Comments and Example

aimtocode

Comments Defination

When writing code in Python, it’s important to make sure that your code can be easily understood by others.

Comment is a good idea to add notes to your programs to explain in natural language what the program is meant. These notes are called comments, and they start with the # symbol:

Therefore, Comments can be used to explain, make the code more readable and prevent execution when testing code.

Single Line Comment

 #This is a comment.
 print("Hello, World!")

Output:

 Hello, World! 

We can also place comments'#' at the end of a line, and Python will ignore the rest of the line:

  print("Hello, World!") #This is a comment.

Output:

 Hello, World! 

Multi Line Comments

To have a multi-line comment in Python, we use triple single quotes at the beginning and at the end of the comment, as shown below.

Example:

 '''
 This is a 
 multi-line
 comment
 '''
 print("Hello, world!")	

Output:

 print("Hello, world!")	

Example:

'''
 We are writing a simple program here
 First print statement.
 This is a multiple line comment.
 '''
 print("Hello World")

 # Second print statement
 print("Welcome to aimtocode?")

 print("Enjoy Learning Python") # Third print statement

Output:

 Hello World
 Welcome to aimtocode?
 Enjoy Learning Python
aimtocode