×

How to comment multiple lines in python?

How to comment multiple lines in python

There are two qualities that come up with every good and successful programmer-

i) Indenting the code

ii) Using comments in the code

Let us see how these two features add weight and sense to your code-

  1. Indentation is a good practice when you are writing a program because it helps us to comprehend the blocks of code present and we can easily rectify it if any sort of problem occurs in terms of its functionality.
  2. Comments are an integral part of our program since they add meaning to it and tell us about why certain keywords or functions are used.

In this article, we will discuss how we can add comments to our python program.

Two types of comments can be written-

  1. Single line comment
  2. A single-line comment provides a succinct one-line description of a single (particular line) of our program.
  3. These comments start with ‘#’ and written after our line of code.

Example-

How to comment multiple lines in python
 #function to print the message Hello World
 def msg():
     print("Hello World")
     print("HELLO WORLD")
     print("hello world")
     print("hELLo WoRld")
 msg()
 msg() 

OUTPUT-

How to comment multiple lines in python
  1. Multiple lines comment

Python doesn’t have a specific syntax for multi-line comments but consecutive lines can be commented using ‘#’.

We can also comment using multiline strings (triple quotes).

Let’s look at an example for a better understanding of how we can add comments in our program-

  1. Single line comments
How to comment multiple lines in python
 a=int(input("Enter the value of a: "))   #taking value of a from user
 b=int(input("Enter the value of b: "))   #taking value of b from user
 add=a+b                                  #adding a and b
 sub=a-b                                  #subtracting a and b
 mult=a*b                                 #multiplying a and b
 div=a/b                                  #dividing a and b
 #displaying results
 print("Result of {} + {} is {}".format(a,b,add))    
 print("Result of {} - {} is {}".format(a,b,sub))
 print("Result of {} * {} is {}".format(a,b,mult))
 print("Result of {} / {} is {}".format(a,b,div)) 

OUTPUT-

How to comment multiple lines in python
  • Multiline Comments

Here we have used a multiline string, let us see what output does it give?

 ''' 
 Tutorial & Examples is a good website for students.
 It covers all the topics and provides a great learning platform.
 Students must go through it once.
 '''
 print ('Python Programmers') 
How to comment multiple lines in python

OUTPUT-

How to comment multiple lines in python

In this program, we have assigned this multiline string to a variable and applied the split() function, on running the given program, it will return a list whose each element is obtained when a ‘/n’ escape character is found.

 a=''' 
 Tutorial & Examples is a good website for students.
 It covers all the topics and provides a great learning platform.
 Students must go through it once.
 '''
 print(a.split('\n')) 
How to comment multiple lines in python

OUTPUT-

How to comment multiple lines in python

Let us look at some examples for a better understanding of how we can use comments in python-

Program to find greatest among three numbers-

 n1=int(input("Enter 1st number: "))  #asking the value from user
 n2=int(input("Enter 2nd number: "))
 n3=int(input("Enter 3rd number: "))
 if(n1>n3 and n1>n2):                  #comparing the two variables
     print("{} is greatest".format(n1))  #displaying results
 elif(n2>n1 and n2>n3):
         print("{} is greatest".format(n2))
 else:
         print("{} is greatest".format(n3)) 

Program to calculate roots of a quadratic equation-

 a=int(input("Enter value of a : "))  #asking the value from user
 b=int(input("Enter value of b: "))
 c=int(input("Enter value of c: "))
 D=(b**2)-(4*a*c)                #calculating value of D
 d=2*a
 if(D>0):                       #comparing according to standard quadratic equation
     print("We will have real roots")
     r1=(-b+D**0.5)/d
     r2=(-b-D**0.5)/d
     print("The roots are {} and {}".format(r1,r2))
 elif(D==0):
     print("We will have equal roots")
     r1=-b/d
     print("Values of root1 and root2 is {}".format(r1))
 else:
     print("We will have imaginary roots") 

Program to print corresponding day of week

 dno=int(input("Enter a number between 1 to 7: ")) #asking the user to input a value
 if(dno==1):            #comparing the value
     print("Sunday")     #displaying the result
 elif(dno==2):
     print("Monday")
 elif(dno==3):
     print("Tuesday")
 elif(dno==4):
     print("Wednesday")
 elif(dno==5):
     print("Thursday")
 elif(dno==6):
     print("Friday")
 elif(dno==7):
     print("Saturday") 

Python program to calculate simple interest.

 p=int(input("Enter the principal: "))     #user enters value of principal
 r=int(input("Enter the rate: "))             #user enters value of rate
 t=int(input("Enter the time: "))            #user enters value of time
 si=p*r*t/100
 print("The value for simple interest is {}".format(si)) 

Python program to calculate the volume of a cylinder.

 pi=3.14
 r=int(input("Enter the value of radius: ")) #user enters the value of radius
 h=int(input("Enter the value of height: "))
 vol=pi*pow(r,2)*h                                     #calculating volume
 print("Volume of cylinder is {}".format(vol))      

Python program to calculate the area of the circle.

 pi=3.14
 r=int(input("Enter the value of radius: ")) #user inputs the value of radius
 area=pi*r*r                                               #calculating area
 print("Area of circle is {}".format(area)) 

Python program to calculate distance between two points.

 x1=int(input("Enter the x-coordinate of first point: ")) #entering x coordinate
 y1=int(input("Enter the y-coordinate of first point: ")) #entering y coordinate
 x2=int(input("Enter the x-coordinate of second point: "))
 y2=int(input("Enter the x-coordinate of second point: "))
 dist=((x2-x1)**2+(y2-y1)**2)**0.5
 print("The calculated distance is {}".format(dist)) 

Related Topics

Python program to display ASCII value of the character

Python program to display ASCII value of the character The full form of ASCII is American Standard Code for Information Interchange. This example explains a program in Python to find the...

1 minute read.

Python Maurer Rose and Rhodonea Curves

In this tutorial, in Python we will make a Maurer Rose example and Rhodonea Curve example. Before we continue to see what precisely is maurer rose or a rhodonea bend...

10 minutes read.

Python Breakpoint

Introduction In Python 3.7, a brand-new created function called breakpoint() was added. Due to the close relationship between both the executable and the code of a debugging component, debugging Python programming...

4 minutes read.

How to Install PIP In Python

How to Install PIP In Python The libraries for Python have made our work easier than we expected. From a simple addition of two numbers to applying algorithms on the big...

4 minutes read.

Python program to find the GCD or HCF

Python program to find the GCD or HCF The largest positive integer that perfectly divides the two numbers is the HCF (Highest Common Factor) or GCD (Greatest Common Deviser). For example,...

5 minutes read.

Mrcnn Python

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

6 minutes read.

Hypothesis Testing in Python

Hypothesis Testing in python is widely used along with statistics. Many libraries in Python are very useful for statistics and machine learning. Libraries like numpy, scripy etc. help in hypothesis testing in...

12 minutes read.

Python Built-in Functions

Python Built-in Functions The Python interpreter has a number of functions and types built into it that are always available. The Python built- in functions are as follow: Methods Explaining abs() The abs() function returns...

6 minutes read.

Python type() Function

Python type() Function The type() function in Python returns the type of an object. The return value is a type object and generally the same object as returned by object.__class__. Syntax class type(object)      ...

1 minute read.

Working with files in Python

Python is an object-oriented high-level programming language. Python has dynamic semantics and has high-level built-in data structures which support dynamic typing and dynamic binding. Python provides rapid development. It has an...

5 minutes read.

Python BytesIO

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.

Python Functionalities of Pillow Module

This instructional exercise is about "Pillow" library, one of the significant libraries of python for picture control. Pillow library of python, is an easy to use and also open source...

14 minutes read.

EDA in Python

The EDA is the exploratory data analysis; the data scientists mainly use the EDA to understand the main features of the data quickly, the variables in python and the relationship...

6 minutes read.

Python Arithmetic Operators

An arithmetic operator is a mathematical operator that is used to operate on two operands. Based on the operator used, action is performed on the operands, and output is delivered. Following...

3 minutes read.

How to Plot Graphs Using Python?

In this article, we are going to learn about how to plot different types of graphs using Python. We are going to use different approaches for every type of graph....

3 minutes read.

Python program to add two number

Python program to add two number This program will add the two numbers and display their sum on the screen. Example: Input: Number1 = 20        Number2 = 30   Output: Sum =...

2 minutes read.

How to Install Tweepy in Python

In this article, we will learn or understand how to install Tweepy in Python and what Tweepy is. Firstly, let’s understand What Tweepy is. As we all know, one of the...

3 minutes read.

Python Basics

In this tutorial, we will learn about the basics of Python, a very famous programming language. This tutorial will help you understand the language better if you start with python....

8 minutes read.

Recursive Multiplication Python

Recursive Multiplication Python In this tutorial, we will learn how to write a Python program to get the multiplication of two numbers using the recursive approach. In the recursive multiplication approach, we...

2 minutes read.

Python Program to Check Leap Year

Python Program to Check Leap Year Leap year: We all know that each year has 365 or 366 days. But whenever a year has 366 days, then the year is said...

2 minutes read.