How To Comment Out Multiple Lines In Python

How To Comment Out Multiple Lines In Python

A comment is a very important and useful part of any programming language. Comments help people interested in coding, whether technical or non-technical, understand the programming code. It plays a very important role in making code readable and understanding how the code works.

In this article, we will learn “How to Comment Out Multiple Lines in Python.” We will also know what comments are, why we need them, and so on. Let’s start.

Need coding assignment or homework help? Hire codeavail experts now!

What is a comment?

A comment is a mechanism or remark by which we can explain our thoughts about any particular action to others.

Take a small example. When we have an idea for something new, we discuss it with others, such as our friends, partners, and so on, and they use some comments and examples to motivate us.

Why do we need comments?

Comments help to explain the actions we take in the program and what we can do with those actions; they fully explain.

You know very well that the compiler and interpreter ignore comments, so it is helpful if you want to explain any specific logic and hard coding to others in the program. Compilers and interpreters do not run those comments.

In programming, we sometimes want to run multiple logic in the same program, but when an error occurs, the logic stops, and the program crashes. In this case, comments are very useful. We can use them to comment (or use the particular code as comment) on that syntax and logic, which is not executed by the compiler and interpreter.

For example

In case 1

In this program, we create three variables, a, b, and c, but we want to print the output of ( d-a). Then, we found only one statement is run. Other is not running because we have yet to define the d variable. 

Get below output 

In case 2

As per the above discussion, we create three variables, a, b, and c, but we want to print the output of (d-a). Then, we found that only one statement was running. Other was not running because we have yet to define the d variable. Now we have put a comment in front of the second statement. After that, our program executes rest statements and ignores the statement where we have used comments.

Output

Hence, if we want to prevent any code from running, we can comment on it.

So, now we will discuss “How to Comment Out Single and Multiple Lines in Python.” let’s start.

Also read: How To Call A Function In JavaScript

Single-line comment use in Python

As you know, Python is a very simple, high-level programming language. It provides many features like those of other languages.  

If you want to comment on a single line in the Python programming language, you can use the hash symbol (#). You can just put the hash code “#” in front of the line where you want comments.

For Example


Program

name = "Max"   # name is a string value
age = 10       # age is a numeric value
 
print(name)
print (age)

Output 

Max

10

Process finished with exit code 0

In this program, we create two variables, name and age, and print the outputs. You can see that we have also mentioned comments for both variables for your information: variable name is a string value, and variable age is a numeric value, which helps to understand the variable data type.

Output

You can see the commented line is not printed in the output. In the Python programming language, this is the simplest way to mention comments for a single line. It is very simple, like we use two slashes “//”  for single-line comments in JavaScript. 

You can also use hash (#) for multiline comments. You can put # after all those lines on which you want to comment.

MultiLine comments used in Python 

Python lacks an inbuilt multiline comment mechanism, as do other programming languages such as C, C++, Java, and others, which provide a multiline comment mechanism that is /************/.

 But we have one method we can use: docstrings for multiline comments in Python programming.

What is Docstring?

A simple approach to connecting documentation with Python modules, functions, classes, and methods is using documentation strings.

There should be a docstring for every function so the program can investigate the comments while running, perhaps as an interactive help system or as metadata.

Docstrings are two types.

  1. ””” “”” ( three Double Quotes)
  2. ‘’’ ‘’’ ( three Single Quotes) 

1. (””” “””) three Double Quotes docstring

We will show you with the help of an example. In this example, we have defined a function named addNumbers and assigned three parameters (num1, num2, and num3) to this function. After that, we will start three double quotes in the docstring for comments, write comments, and close three double quotes in the docstring. After that, we return the function value (num1 + num2 + num3). After that, we print the function value with a passing argument for parameters. 

As you can see, we used to print(addNumbers.__doc__) to print a docstring. We’ll use it to print, first type print, then add parentheses after the function’s name, addNumbers. After that, “. “dot double-underscore “doc,” then double-underscore “close parentheses.”

Program

def multiplication(num1, num2, num3):
    """
    A function that returns the Multiplication of
    3 numbers
    """
    return num1 * num2 * num3
print(multiplication(2, 3, 4))
 
print(multiplication.__doc__)

Output

24

    A function that returns the Multiplication of

    3 numbers

2. (‘’’ ‘’’) three Single Quotes docstring

When using three single quotes in a docstring, almost all the processes are the same; we only have to replace three double quotes with three single quotes. You can see the below example and the program.

Program

def multiplication(num1, num2, num3):
    """
    A function that returns the Multiplication of
    3 numbers
    """
    return num1 * num2 * num3
print(multiplication(2, 3, 4))
 
print(multiplication.__doc__)

Output

24

    A function that returns the Multiplication of

    3 numbers

Drawback of docstrings

When you used docstrings for comments, indentation also still matters. Because if you have yet to follow proper indentation for docstrings (if you will use 4 spaces for indentation), then they will return an indentation error.

For example

Program

def multiplication(num1, num2, num3):
  """
    A function that returns the Multiplication of
    3 numbers
    """
    return num1 * num2 * num3
print(multiplication(2, 3, 4))
 
print(multiplication.__doc__)

In this example, you can see we have used space when we start docstrings and close docstring. That is the reason we have found an Indentation Error.

Output

    return num1 * num2 * num3

IndentationError: unexpected indent

Process finished with exit code 1

Important information

Some developers use docstrings without creating classes and objects. They use docstring only for multiline comments.

We suggest you only use docstrings once you use classes and objects because docstrings play a big role.

Below is an example of how they can do

In this program, we have swapped the values of variables. Let’s start by saying that we have created five variables named name1, age1, name2, age2, and temp with passing arguments. Harold, 20; Gloria, 50; We also mention commenting with the help of docstring without creating classes and objects. Now we check the result of this program.

Output

You can see that this program output does not show comments in the output.

Conclusion

We have discussed “How to Comment Out Multiple Lines in Python.”  in this article, and we have also discussed docstrings. This information is very helpful to you. Now you learned why comments are important. Why we need them and how we can use them in a Python programming language

FAQ (Frequently Asked Questions)

What is commenting in Python?

You can use the break statement to exit a loop early in JavaScript. The break statement can be used for loops, while loops, and do…while loops. When the break statement is executed, the loop terminates, and any code after the loop is executed.

How can I comment out multiple lines in Python?

In Python, the pound/hash sign (#) can be used at the beginning of each line to comment out many lines at once.

Is there another way to comment out multiple lines in Python?

In Python, triple quotes are another method for commenting out several lines (either single quotes or double quotes). In a single statement, you can use this to comment out numerous lines.

Can I use the pound symbol and triple quotes to comment on multiple lines in Python?

In Python, you can comment out several lines using either the pound sign or triple quotes. Use the proper comment syntax for the precise lines you wish to remark out.