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-
- 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.
- 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-
- Single line comment
- A single-line comment provides a succinct one-line description of a single (particular line) of our program.
- These comments start with ‘#’ and written after our line of code.
Example-

#function to print the message Hello World def msg(): print("Hello World") print("HELLO WORLD") print("hello world") print("hELLo WoRld") msg() msg()
OUTPUT-

- 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-
- Single line comments

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-

- 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')

OUTPUT-

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'))

OUTPUT-

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))