"""Multiline Comments in Python"""
Introduction
In programming, a programmer can provide explanations for their code by adding comments. It is an essential component of every software since it aids in code debugging during development.
What is a block comment?
The machine ignores the content in between the start and end of a block comment, which is a paragraph of text with these two signs. When working in a team, block comments are one method of explaining the code. A block comment is naturally indented at the same level as the code block by the developer. In a programme, comments are drawn lines of text that are not actually executed. Let's look at how to make a Python comment block.
Python block comment
Python programmers should append a #(octothorpe) to each line to generate comment blocks. Code can be made more legible by comments, which also stop the code from running while being tested. Like other languages, Python also enables multi-line comments. The majority of computer languages in use today provide syntax for block comments that span many lines of text. C or Java, for instance. Additionally, the majority of Python online IDEs come with a feature that automatically supports pound-sign block comments. The major two uses of comment blocks among programmers are as follows.
- Explaining the code
- Testing the code
# Comments in Python
In Python, you must prefix each new code line with the commenting character "#" (octothorpe). It instructs the Python compiler to skip over it and move on to the following line. When a comment begins with a #, the Python compiler won't execute it.
# A comment |
In the preceding example, we commented on the code using the hash symbol (#). The # only functions when one line of code, not several, is used. When a comment appears on the same line as a statement, it is referred to as an inline comment.
Example
# multiplication sign print(3 * 7) |
When a comment is added at the end of a line, the Python interpreter ignores the content after the comment and only executes the content before it.
Python multiline comment
In Python, append a # to each line to indicate that the remark should be multi-line. If you begin each line with the # symbol in a row, you will receive multiline comments. There isn't a built-in method for writing multiline comments in Python. Thus, you must compose a series of one-line comments. Java, C, and C++ are all capable of producing multi-line code.
/* |
But Python does not allow you to accomplish this. Use successive single-line comments separated by # to comment on several lines at once in Python. A "#" is referred to as an octothorpe. Sadly, Python doesn't enable appropriate multi-line comments since it requires more work to comment out many code lines.
# To print the value, use the print() function |
Output 21 |
Therefore, you could insert a # for each line of a multi-line comment.
Using Multi-line Strings as Comments
Using multi-line strings, sometimes known as docstrings, is another technique to make multi-line comments in Python. Despite having the same result, this is utilized for documentation strings rather than block comments. It is fine as a temporary remedy if you are only making passing remarks. Python supports two different types of docstrings: single-line and multi-line. To make a block comment, multi-line docstrings will be used.
""" To print the value, use the print() function |
Output |
One-line docstrings in Python
One line can contain a single docstring. In Python, a single-line docstring always starts and ends with triple quotes (""").
""" Use * for multiplication of a number """ |
Output |
As you can see, we've made a representation of a multi-line Python comment using triple-quoted strings. You must appropriately indent the initial " UNK> UNK> in order to avoid receiving a SyntaxError. It's always seen as a good practice to make your opinion succinct, clear, and informative. The main reason for comments is to save you and the other developers working on the project time and effort.