[Tut] Is There A Way To Create Multiline Comments In Python? - Printable Version +- Sick Gaming (https://www.sickgaming.net) +-- Forum: Programming (https://www.sickgaming.net/forum-76.html) +--- Forum: Python (https://www.sickgaming.net/forum-83.html) +--- Thread: [Tut] Is There A Way To Create Multiline Comments In Python? (/thread-99167.html) |
[Tut] Is There A Way To Create Multiline Comments In Python? - xSicKxBot - 04-08-2022 Is There A Way To Create Multiline Comments In Python? <div><p class="has-background" style="background-color:#ccfdf8"><strong>Summary: </strong>You can use consecutive single-line comments (using <code>#</code> character) to create a block of comments (multiline comments) in Python. Another way is to use <code> """ </code>quotes to enclose the comment block.</p> <hr class="wp-block-separator" /> <p><strong>Problem Statement:</strong> How to create multiline comments in Python?</p> <p>Other programming languages like C++, Java, and JavaScript, have an inbuilt mechanism (block comment symbols) for multiline comments, but there is no built-in mechanism for multiline comments in Python. Hence, the following code won’t work:</p> <pre class="wp-block-code"><code>/* This is a multiline Comment in Python */</code></pre> <p><strong>The above code will throw an error in Python.</strong> However, there are still some workarounds to using the multiline comments in Python. Let’s look at the different methods to do this in this article.</p> <ul class="has-base-2-background-color has-background"> <li><strong>Quick Trivia on Comments</strong>: <ul> <li> Comments are a very important part of every programming language as it explains the logic used in the code. The developers provide these comments to make it more readable and for the users to get a better understanding of the code. The comments don’t run as they are ignored by the interpreters and compilers. Comments also help us while we are debugging, i.e., when there are two lines of code, we can comment one out to prevent it from running.</li> </ul> </li> <li><strong>What is a multiline comment in Python</strong>? <ul> <li>A multiline comment in Python is a comment that generally expands to multiple lines, i.e., multiline comments are the comments that expand to two or more lines in the source code.</li> </ul> </li> </ul> <h2><strong>Method 1: Using Multiple Single Line Comments</strong></h2> <p>We can use the multiple single-line comments to create multiline comments in Python. But first, you must know how to make a single-line comment is in Python. The <strong>Hash character (#)</strong> is used to make single-line comments in Python. The commented line does not get printed in the output.</p> <p><strong>Example:</strong></p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># print("Hello Finxters") print("Learning to create multiline comments in Python")</pre> <p><strong>Output:</strong></p> <pre class="wp-block-code"><code>Learning to create multiline comments in Python</code></pre> <p>Now let’s create multiline comments using consecutive single line comments:</p> <p><strong>Example:</strong></p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="2-5" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">print("Learning to create multiline comments in Python") # print("Hello Finxters") # print("This is a") # print("Multiline comment") # print("in Python") print("End of program")</pre> <p><strong>Output:</strong></p> <pre class="wp-block-code"><code>Learning to create multiline comments in Python End of program</code></pre> <p>As we can see that the commented lines are ignored by the parser in Python, thereby creating a block of comments.</p> <p><strong>Discussion: </strong>Using single-line comments to comment out every line of a multiline comment individually becomes a very tedious process. Hence this method is not recommended to be used when you are not using any modern editor. However, most of the new code editors have a shortcut for block commenting in Python. You can just select a few lines of code using shift and the cursor keys and then press cmd + / (This shortcut may differ depending upon the editor you are using) to comment them out all at once. You can even uncomment them easily by simply selecting the block of comments and pressing the cmd + / keyboard shortcut.</p> <h2><strong>Method 2: Using Docstrings Or Multiline Strings</strong></h2> <p>We can create the multiline comments using multiline strings or docstrings in Python. This method has the same effect but is generally used for documentation strings, not block comments. However, if you want to comment things out temporarily, you can use this method. Python has two types of docstrings-<br />1) One-Line docstrings<br />2) Multiline docstrings. </p> <p>To create a block comment, we use the multiline docstrings. Let’s create multiline comments using docstrings in the following example:</p> <p><strong>Example 1:</strong></p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="2-7" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">print("Learning to create multiline comments in Python") ''' print("Hello Finxters") print("This is a") print("Multiline comment") print("in Python") ''' print("End of program")</pre> <p><strong>Output:</strong></p> <pre class="wp-block-code"><code>Learning to create multiline comments in Python End of program</code></pre> <p><strong>Example 2:</strong> Suppose you want to define a block comment inside a function using docstrings you have to do it the following way:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="3-7" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def multiply(x, y): res = x * y """ This is a multiline comment indented properly This function returns the result after multiplying the two numbers """ return res print("The multiplication of the two numbers is", multiply(10, 5))</pre> <p><strong>Output:</strong></p> <pre class="wp-block-code"><code>The multiplication of the two numbers is 50</code></pre> <ul> <li><strong>Caution: </strong> <ul> <li>You must always ensure that you have used the indentation for the first <code>"""</code> correctly; otherwise, you may get a <code data-enlighter-language="generic" class="EnlighterJSRAW">SyntaxError</code>.</li> <li>Also, if you are opening a multiline comment using three double quotes <code>"""</code> then you must ensure that you enclose the block with exactly three double quotes as well. If you do nt follow this convention, you will get an error again. For example – if you open a multiline comment with three double quotes and close it using three single quotes, then you will get an error.</li> </ul> </li> </ul> <p><strong>Example I: </strong>If you don’t intend <code>"""</code> properly, you may get the following error:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def multiply(x, y): res = x * y """ This is a multiline comment indented properly This function returns the result after multiplying the two numbers """ return res print("The multiplication of the two numbers is", multiply(10, 5))</pre> <p><strong>Output:</strong></p> <pre class="wp-block-code"><code>File "main.py", line 10 return res ^ IndentationError: unexpected indent</code></pre> <p><strong>Example II: </strong>Let’s visualize what happens when there is a mismatch between the type of triple quotes used.</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def multiply(x, y): res = x * y """ This is a multiline comment indented properly This function returns the result after multiplying the two numbers ''' return res print("The multiplication of the two numbers is", multiply(10, 5))</pre> <p> <strong>Output:</strong></p> <pre class="wp-block-code"><code> File "C:\Users\SHUBHAM SAYON\PycharmProjects\Finxter\General\rough.py", line 10 print("The multiplication of the two numbers is", multiply(10, 5)) ^ SyntaxError: EOF while scanning triple-quoted string literal</code></pre> <p><strong>Note: </strong>You must always be careful where you place these multiline comments in the code. If it is commented right after a class definition, function, or at the start of a module, it turns into a docstring that has a different meaning in Python.</p> <p><strong>Example:</strong></p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def multiply(x, y): """ This is a multiline comment made right after the function definition It now becomes a function docstring associated with the function object that is also accessible as runtime metadata """ res = x * y return res print("The multiplication of the two numbers is", multiply(10, 3))</pre> <p><strong>Output:</strong></p> <pre class="wp-block-code"><code>The multiplication of the two numbers is 30</code></pre> <p><img src="https://s.w.org/images/core/emoji/13.1.0/72x72/1f58b.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" />The difference between the comment and the parser is that the comment is removed by the parser, whereas a docstring can be accessed programmatically at runtime and ends up in the byte code. </p> <h2><strong>Conclusion</strong></h2> <p>Therefore, in this tutorial, we learned two ways of creating multiline comments in Python –<br />➨Using consecutive single-line comments. <br />➨Multiline strings (docstrings).</p> <p>That’s all for this article. I hope you found it helpful. Please stay tuned and subscribe for more interesting articles and tutorials in the future. Happy learning!</p> <p class="has-text-align-center has-base-2-background-color has-background" style="font-size:15px"><img src="https://s.w.org/images/core/emoji/13.1.0/72x72/1f58b.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" />Authors: <strong>Rashi Agarwal</strong> and <strong>Shubham Sayon</strong></p> <hr class="wp-block-separator" /> <p><strong>Recommended Read: </strong></p> <ul> <li><strong><a href="https://blog.finxter.com/multi-line-strings/" target="_blank" rel="noreferrer noopener">Python Multi-Line Strings</a></strong></li> <li><strong><a href="https://blog.finxter.com/python-comments/" target="_blank" rel="noreferrer noopener">Python Comments — 2-Minute Guide with Exercise</a></strong></li> </ul> </div> https://www.sickgaming.net/blog/2022/03/30/is-there-a-way-to-create-multiline-comments-in-python/ |