Python Comments

Table Of Contents

  1. What Is Python Comment?
  2. Single Line Comments.
  3. Multiple Line Comments.

(1) What Is Python Comments?

  • Comments are descriptions that a programmer writes for a better understanding of the intent and functionality of the program.
  • Using comments in programs makes our code more understandable. It makes the program more readable which helps us remember why certain blocks of code were written.
  • Other than that, comments can also be used to ignore some code while testing other blocks of code. This offers a simple way to prevent the execution of some lines or write a quick pseudo-code for the program.
  • They are completely ignored by the Python interpreter.

(2) Single Line Comments

  • In Python, we use the hash symbol (#) to write a single-line comment.
  • Single-line comments are the short description of the code.

Example-1

# This Is A Single Line Comment
def addition():
    sum = 30 + 50
    print(sum)
addition()
Output: 80

Example-2

# This Is A Single Line Comment
def subtraction():
    sub = 60 - 25
    print(sub)
subtraction()
Output: 35

(3) Multiple Line Comments

  • Python doesn’t offer a separate way to write multiline comments. However, there are other ways to get around this problem.
  • We can use (#) at the beginning of each line of comment on multiple lines.
  • We can use String Literals (”’ ”’) or (“”” “””) to write multiple-line comments.

Example-1

# It Is A
# Multiline 
# Comment
def addition():
    sum = 30 + 50
    print(sum)
    
addition()
Output: 80

Example-2

''' 
It IS A 
Multiline
Comment

'''
def subtraction():
    sub = 60 - 25
    print(sub)
    
subtraction()
Output: 35

Example-3

""" 
It IS A 
Multiline
Comment

"""
def multiplication():
    mul = 10 * 10
    print(mul)
    
multiplication()
Output: 100

Leave a Reply

Your email address will not be published. Required fields are marked *