Python: Comments


Comments are the lines that are skipped during the execution of a program. There are two types of comments available in python:

  • Single Line Comment
  • Multi Line Comment

Single Line Comment

In Python, a Single line comment starts with the ‘#’ symbol and extends till the end of the line. Comments can start from the beginning of the line and middle of the line, but they should not be a part of the string literal.

Example:

#program to demonstrate explicit type conversion 
num1=10 #variable of integer type
num2="20" #variable of string type
result=num1+int(num2) #using explicit conversion 	print(result)

All the above statements (start with a # sign) mentioned in the code are examples of the single-line comments.

Multi Line Comment

In Python, Multiline comment which starts with ”’ or “”” and ends with ”’ or “”” respectively. Which is mainly used for documentation purposes.

Example:

'''
used for: Demonstrating comments 
This is the first way of using multi-line comment
'''
	
"""
used for: Demonstrating comments 
the second way of using multi-line comment	
"""

All the above statement (start with ”’ and “”” )mentioned in code are an example of the multiline comment. Such types of comments generally use in class level or method level documentation.

One thought on “Python: Comments”

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s