×

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

Compound Interest GUI Calculator using PyQt5 in Python

GUI: One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs...

6 minutes read.

Python Variables, Constants and Literals

Python is today's most valuable, easy-to-implement and sought-out programming language. Nowadays, developers want to focus on implementing the program rather than spending time with complex programs. So, for this reason,...

17 minutes read.

Python Function

Python Function A Python function is a reusable, organized block of code that is used to perform the specific task. The functions are the appropriate way to divide an extensive program into a...

7 minutes read.

Python str() function

Python str() function The str() function in Python converts the specified value into a string. Syntax class str(object='')           or class str(object=b'', encoding='utf-8', errors='strict') Parameter object:  This parameter represents any object to convert the given...

1 minute read.

Python getattr() function

Python getattr() function The getattr() function in Python returns the value of the named attribute for the given object. Syntax getattr(object, name[, default]) Parameter object: It is a required parameter which represents an object. name: This parameter represents the...

1 minute read.

Python JSON Schema

Python-jsonschema JSON: JSON stands for JavaScript Object Notation. It is a text-based format that represents structured data and can be used to interchange data among various applications. This is self-defining language...

5 minutes read.

What is Python compiler GDB?

The source code of one programming language is converted into machine code, bytecode, or another programming language by a compiler, a specialised software. A compiler is a tool that converts high-level...

3 minutes read.

Python String splitlines() method

Python String splitlines() method The string.splitlines() method in Python splits the specified string and returns a list of the lines in the string, breaking at line boundaries.  Syntax splitlines([keepends]) Parameter keepends(optional): This parameter specifies if...

1 minute read.

Is Python Case-sensitive when Dealing with Identifiers

Yes, Python is a case-sensitive language while dealing with identifiers. Python is one of the top trending, widely-used programming languages. Python is a general-purpose programming language. It is a case-sensitive...

6 minutes read.

One Liner If-Else Statements in Python

Before learning one liner if else Python, let us first know what Python is. Python is a broadly helpful, object-oriented and dependable language having a large number of utilizations from...

4 minutes read.

Python Data Visualization

In this tutorial, we will understand what data visualization means in python. Further, we will see different methods of visualizing data in python. Data visualization In a non-technical language, it is a...

4 minutes read.

Python Syntax

Python is a strong object-oriented programming language that is simple to learn. Python was created to be a very readable programming language. The syntax of the Python programming language is...

8 minutes read.

How to Install Matplotlib in Python?

How to Install Matplotlib in Python The speed at which the enormous amount of data is generating has become a huge aid in understanding what's going on currently in the market....

4 minutes read.

Python Sending Email

Python Sending Email Simple Mail Transfer Protocol (SMTP) is used to handle sending e-mail and routing e-mail between mail servers. When we send an email either form a web-application or from a local software...

3 minutes read.

Hog Descriptor Opencv Python

Python programming language: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a...

3 minutes read.

Python float()

Python float() class The float() class in Python returns a floating point number constructed from a number or string x. Syntax class float([x]) Parameter x: This parameter represents a number or a string that can be converted...

1 minute read.

Imread Python

In this walkthrough, we'll go over the specifics of using the imread() method of OpenCV-Python and various methods for loading images. Imread is an Image reading library. One of the most helpful...

7 minutes read.

Python vs HTML

Python and HTML are not comparable since they are two separate categories of programming languages. Building the structures and layouts of a web page or app requires the usage of HTML,...

8 minutes read.

Programs for Printing Pyramid Patterns in Python

<!-- wp:paragraph --><p>Python supports printing patterns using basic for loops. The number of rows is handled by the first outer loop, while the number of columns is handled by the...

9 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.