Python comment multi lines

Multiple line comment in Python [duplicate]

Is there a way to give multiple line comments in Python? Like it is in case of C/C++ : /*comment*/ . Or does it have to be marked «#» in front of every line?

It’s worth pointing out (despite the tag), that in C and C++ a multi-line comment is given by opening /* and closing */ , not /- .

2 Answers 2

''' This is a multiline comment. I can type here whatever I want. ''' 

Python does have a multiline string/comment syntax in the sense that unless used as docstrings, multiline strings generate no bytecode — just like #-prepended comments. In effect, it acts exactly like a comment.

On the other hand, if you say this behavior must be documented in the official docs to be a true comment syntax, then yes, you would be right to say it is not guaranteed as part of the language specification.

In any case your editor should also be able to easily comment-out a selected region (by placing a # in front of each line individually). If not, switch to an editor that does.

Programming in Python without certain text editing features can be a painful experience. Finding the right editor (and knowing how to use it) can make a big difference in how the Python programming experience is perceived.

Not only should the editor be able to comment-out selected regions, it should also be able to shift blocks of code to the left and right easily, and should automatically place the cursor at the current indentation level when you press Enter. Code folding can also be useful.

Источник

Python Multiline Comment – How to Comment Out Multiple Lines in Python

Kolade Chris

Kolade Chris

Python Multiline Comment – How to Comment Out Multiple Lines in Python

Commenting is an integral part of every programming language. With comments, you get a better understanding of your own code, make it more readable, and can help team members understand how it works.

Comments are ignored by compilers and interpreters, so they don’t run.

Apart from making your code more readable, comments can also help while you’re debugging – if you have two lines of code, you can comment out one to prevent it from running.

Just like other programming languages, Python supports comments.

The problem is that Python doesn’t have a built-in mechanism for multi-line comments.

So in this article, I won’t just show you how to make single-line comments in Python – I’ll also show you the workaround for making multi-line comments.

Читайте также:  Python cannot schedule new futures after shutdown

How to Make Single Line Comments in Python

To make single-line comments in Python, prepend each line with a hash ( # ).

# print("Hello world") print("Hello campers") 

As you can see, the commented line wasn’t printed in the output.

How to Make Multi-line Comments in Python

Unlike other programming languages such as JavaScript, Java, and C++ which use /*. */ for multi-line comments, there’s no built-in mechanism for multi-line comments in Python.

To comment out multiple lines in Python, you can prepend each line with a hash ( # ).

# print("Hello world") # print("Hello universe") # print("Hello everyone") print("Hello campers") 

With this approach, you’re technically making multiple single-line comments.

The real workaround for making multi-line comments in Python is by using docstrings.

If you use a docstring to comment out multiple line of code in Python, that block of code will be ignored, and only the lines outside the docstring will run.

""" This is a multi-line comment with docstrings print("Hello world") print("Hello universe") print("Hello everyone") """ print("Hello campers") 

NB: One thing to note is that while using doctsrings for commenting, indentation still matters. If you use 4 spaces (or a tab) for indentation, you will get an indentation error.

For example, this will work:

def addNumbers(num1, num2, num3): """ A function that returns the sum of 3 numbers """ return num1 + num2 + num3 print(addNumbers(2, 3, 4)) # Output: 9 
def addNumbers(num1, num2, num3): """ A function that returns the sum of 3 numbers """ return num1 + num2 + num3 print(addNumbers(2, 3, 4)) 

So your IDE will throw the error » IndentationError: expected an indented block «.

Conclusion

Since there’s no built-in support for multi-line comments in Python, this article demonstrates how you can use docstrings as a workaround.

Still, you should generally stick to using regular Python comments using a hash ( # ), even if you have to use it for multiple lines. This is because docstrings are meant for documentation, and not for commenting out code.

If you found this article helpful, consider sharing it with your friends and family.

Источник

Python Multiline Comments (2 Different Options)

Python Multiline Comments Cover Image

In this tutorial, you’ll learn how to use Python to create multiline comments. While Python itself doesn’t support multiline comments, there are two options that let you span your comments over multiple lines. You learn how Python comments work and how to keep them going across many lines. You’ll also learn how to use multiline strings to create multi-line comments.

Many programming languages offer multi-line comments. Some of them, for example will use a /* comment */ notation that looks like this:

/* This is a multi line comment in JavaScript */

Python, unfortunately, doesn’t offer this type of comment. But you do have options to let your comments go across multiple lines.

Читайте также:  Html верстка макетов дизайна

The Quick Answer: Use # to Create Multiline Comments in Python

Quick Answer - Python Multiline Comments

Creating Python Comments

Python provides a single option to create a comment, using the pound sign, sometimes called the hash symbol, # .

Comments in programming languages are used to explain code and provide context. They often help make your code more readable by explaining the why of your code (i.e., why you are writing your code the way you are). Sometimes comments are also used to prevent code from running, and this is often referred to as “commenting code out”.

You have a number of different options of where you place the pound symbol:

  1. If you place the # symbol at the beginning of a line, the entire line becomes a comment
  2. If you place the # somewhere along the line of code, then everything that follows it becomes a comment

Let’s take a look at a few examples:

# Creating Comments in Python # The single hashtag is used to create a single-line comment in Python a = 3 # You can use them in-line, to explain what you're doing # Placing them at the front, though, makes the whole line a comment

In the next section, you’ll learn how to use the single-line comment to create a multiline comment in Python.

Want to learn more about Python list comprehensions? Check out this in-depth tutorial that covers off everything you need to know, with hands-on examples. More of a visual learner, check out my YouTube tutorial here.

Python Multiline Comments with Consecutive Single-Line Comments

While Python doesn’t support multiline comments, you can use single-line comments to make your comments span multiple lines. What we can do to create a comment that goes across multiple lines is simply to comment out multiple lines.

Let’s see what this looks like in Python:

# This is a comment that starts on one line # and keeps going on another # and maybe even one more

This is actually the approach that’s recommended by Python’s PEP-8 style guide. It also represents the only “real” way to create comments in Python. The approach that follows this section isn’t “really” a comment but rather a workaround.

While it may seem really overkill to have to comment out multiple lines. Many code editors like VS Code provide keyboard shortcuts to do this. For example, in VS Code, you can simply use CTRL+/ or CMD+/ to comment out a line.

Learning how to use these keyboard shortcuts can make easy work of something that feels tedious. Because of this, this operation becomes almost second nature and this makes it easier to live with the lack of official multi-line comments.

In the next section, you’ll learn how to use Python multiline strings not assigned to variables to emulate multiline comments in Python.

Читайте также:  Java pattern get all matches

Want to learn how to use the Python zip() function to iterate over two lists? This tutorial teaches you exactly what the zip() function does and shows you some creative ways to use the function.

Python Multiline Comments with Multiline Strings

A simple way that we can emulate multiline comments in Python is to use multiline strings. The way that we can create strings that span multiple lines is to use triple quotation marks, either with single quotes or double quotes.

Let’s take a look at how we can Python to create strings that span multiple lines:

a_multi_line_strin = ''' this is a string that spans multiple lines''' another_multi_line_string = """ this is another string that spans multiple lines"""

An interesting string about Python strings is that the Python interpreter ignores any strings that aren’t assigned to variables, meaning that they are treated indirectly as comments.

Aren’t these Python docstrings?

You may notice that these triple quote look very similar to Python docstrings. Docstrings are used to document a function and are created using triple quotes.

When you do not create triple quotes indented as the first line of a function, they are treated as simple strings.

Let’s see what the difference looks like in Python

def some_function(): """This is a docstrinfg to explain a function""" def another_function(): """This docstring will throw an error since it's not indented properly.""" some_string = """This is a string that's assigned to a variable and spans multiple lines.""" """Because this string isn't assigned to a variable, it will be treated as a multi-line comment and will be ignored."""

You’ll notice four different scenarios above:

  1. A proper docstring, with correct indentation and following a function definition
  2. An improperly indented docstring that will raise an error
  3. A multi-line function assigned to a variable
  4. An unassigned multi-line string that behaves like a comment

Conclusion

In this tutorial, you learned how to use Python to create multi-line comments. While Python itself doesn’t have multiline comments, you are able to create them using two different methods. You learned how to use Python to span single line comments over multiple lines and how to use multiline strings to create comments.

Comments are incredibly helpful tools in programming. They allow us to document code, providing the rationalization as to why we do things. They also allow us to provide explanations of how complex pieces of code may work. They also serve an unintended purpose of allowing us to “comment out” certain sections of our code. This means to take code and turn it into comments so that it will not run when the program is run.

To learn more about comments in Python, check out the official documentation here.

Источник

Оцените статью