Home Python How to Comment Out Multiple Lines In Python

How to Comment Out Multiple Lines In Python

0
How to Comment Out Multiple Lines In Python

In this article, we will learn how can I do comment out multiple lines of Python.

Various editors can do this too—Atom, VS Code, and even Notepad++ all have implied backup courses of action for block commenting in Python.

Contents

How to comment out multiple lines in Python?

Option 1:

  • Your best option for commenting out different lines of code in Python is to simply use a # single-line comment on each line:

This is a "block comment" in Python, made

out of a couple of single-line comments.

Pretty exceptional, eh?

Answer = 42

  • Most Python projects follow this style and Python’s PEP 8 style guide in like manner favors reiterated single-line comments.
  • This can be used as a general rule.
  • This is moreover the most ideal approach to communicate “certifiable” comment blocks in Python that are ignored by the parser.
  • If it disturbs you that Python doesn’t maintain fitting multiline comments since you think it requires more effort to comment out different lines of code
  • Most code editors have a backup course of action for block commenting.
  • In the Sublime Text improvement plan basically, a few lines using shift and the cursor keys (or the mouse) and thereafter hit cmd +/to comment them out simultaneously.
  • This even works in reverse, that is, pick a square of single-line comments and when hit the cmd +/console simple course the whole square gets uncommented again.
  • Managing your Python comments genuinely is an assignment, and this administrator feature can save you hours of your time.

Option 2

Using Multi-line string as a comment:

  • Python multi-line remark is a piece of text encased in a delimiter (“”) on each finish of the remark. Again there ought to be no void area between the delimiter (“”).
  • They are helpful when the remark text doesn’t find a way into one line; accordingly needs to range across lines. Multi-line remarks or passages fill in as documentation for others perusing your code.
  • Another choice for expressing “legitimate” multi-line remarks in Python is to utilize multi-line strings with the “”” punctuation inventively.
  • Here’s a model:

“”” This is a “block remark” in Python, made

Out of a multi-line string consistent.

This really functions admirably! “””

Answer = 42

  • As should be obvious, you can utilize triple-cited strings to make something that takes after a multiline remark in Python.
  • You simply need to ensure you indent the first “”” accurately, else you’ll get a SyntaxError. For instance, on the off chance that you’d prefer to characterize a square remark inside a capacity with this procedure you need to do it like this:

def add_stuff(a, b):

result = a + b """

Presently we return the outcome, small!

Yahoo! I’m so energized I can’t contain

My euphoria to only a couple of lines! “””

Bring result back

  • Simply remember that this procedure doesn’t make “valid” remarks.
  • This just embeds a book steady that doesn’t do anything. It’s equivalent to embeddings a standard single-line string someplace in your code and never getting to it.

Reasons to Comment Out Multiple Lines In Python

Comments are important in any programming language because they allow you to document your code and explain what it is doing. Comments also help other programmers (or future you) who may be looking at your code understand what is going on.

But sometimes, you may want to temporarily comment out a group of lines of code. Maybe you’re testing something and don’t want the code to run, or maybe you’ve made a mistake and need to fix it. In Python, there are two ways to comment out multiple lines of code: the # symbol or the “”” symbol.

The # symbol is used to mark single line comments. To comment out multiple lines of code, use the “”” symbol instead.

Conclusion

Python doesn’t uphold multiline comments.

In any case, you can utilize multi-line docstrings, Consecutive Single-line Comments, and Multi-line strings as comments as multiline remarks.

Guido van Rossum, the maker of Python, likewise suggested this.

It’s a decent practice to keep your remark clear, brief, and illustrative.

A definitive objective is to save time and energy for you and different designers who will deal with the code later.

Read More: How To Install NumPy In Python?

LEAVE A REPLY

Please enter your comment!
Please enter your name here