{"id":25,"date":"2024-01-24T13:03:50","date_gmt":"2024-01-24T13:03:50","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=25"},"modified":"2024-01-24T13:03:51","modified_gmt":"2024-01-24T13:03:51","slug":"python-comments","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/24\/python-comments\/","title":{"rendered":"Python Comments"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn how to add comments to your code. And you\u2019ll learn various kinds of Python comments including block comments, inline comments, and documentation string.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to Python comments<\/h2>\n\n\n\n<p>Sometimes, you want to document the code that you write. For example, you may want to note why a piece of code works. To do it, you use the comments.<\/p>\n\n\n\n<p>Typically, you use comments to explain&nbsp;formulas, algorithms, and complex business logic.<\/p>\n\n\n\n<p>When executing a program, the Python interpreter ignores the comments and only interprets the code.<\/p>\n\n\n\n<p>Python provides three kinds of comments including block comment, inline comment, and&nbsp;documentation&nbsp;string.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python block comments<\/h2>\n\n\n\n<p>A block comment explains the code that follows it. Typically, you indent a block comment at the same level as the code block.<\/p>\n\n\n\n<p>To create a block comment, you start with a single hash sign (<code>#<\/code>) followed by a single space and a text string. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code><em># increase price by 5%<\/em> price = price * 1.05<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Python inline comments<\/h2>\n\n\n\n<p>When you place a comment on the same line as a statement, you\u2019ll have an inline comment.<\/p>\n\n\n\n<p>Similar to a block comment, an inline comment begins with a single hash sign (<code>#<\/code>) and is followed by a space and a text string.<\/p>\n\n\n\n<p>The following example illustrates an inline comment:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>salary = salary * 1.02 <em># increase salary by 2%<\/em><\/code><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Python docstrings<\/h2>\n\n\n\n<p>A documentation string is a\u00a0string\u00a0literal that you put as the first lines in a code block, for example, a\u00a0function.<\/p>\n\n\n\n<p>Unlike a regular comment, a documentation string can be accessed at run-time using\u00a0\u00a0<code>obj.__doc__<\/code>\u00a0attribute where\u00a0<code>obj\u00a0<\/code>is the name of the\u00a0function.<\/p>\n\n\n\n<p>Typically, you use a documentation string to automatically generate the code documentation.<\/p>\n\n\n\n<p>Documentation strings is called docstrings.<\/p>\n\n\n\n<p>Technically speaking, docstrings are not the comments. They create anonymous variables that reference the strings. Also, they\u2019re not ignored by the Python interpreter.<\/p>\n\n\n\n<p>Python provides two kinds of docstrings: one-line docstrings and multi-line docstrings.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1) One-line docstrings<\/h3>\n\n\n\n<p>As its name implies, a one-line docstring fits one line. A one-line docstring begins with triple quotes (<code>\"\"\"<\/code>) and also ends with triple quotes (<code>\"\"\"<\/code>). Also, there won\u2019t be any blank line either before or after the one-line docstring.<\/p>\n\n\n\n<p>The following example illustrates a one-line\u00a0docstring in\u00a0the\u00a0<code>quicksort()<\/code>\u00a0function:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>def quicksort(): \"\"\" sort the list using quicksort algorithm \"\"\" ...<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2) Multi-line docstrings<\/h3>\n\n\n\n<p>Unlike a one-line docstring, a multi-line docstring can span multiple lines. A multi-line docstring also starts with triple quotes (<code>\"\"\"<\/code>) and ends with triple quotes (<code>\"\"\"<\/code>).<\/p>\n\n\n\n<p>The following example shows you how to use multi-line docstrings:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>def increase(salary, percentage, rating): \"\"\" increase salary base on rating and percentage rating 1 - 2 no increase rating 3 - 4 increase 5% rating 4 - 6 increase 10% \"\"\"<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Python multiline comments<\/h2>\n\n\n\n<p>Python doesn\u2019t support multiline comments.<\/p>\n\n\n\n<p>However, you can use multi-line docstrings as multiline comments.\u00a0Guido van Rossum, the creator of Python, also recommended this.<\/p>\n\n\n\n<p>It\u2019s a good practice to keep your comment clear, concise, and explanatory. The ultimate goal is to save time and energy for&nbsp;you and other developers who will work on the code later.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn how to add comments to your code. And you\u2019ll learn various kinds of Python comments including block comments, inline comments, and documentation string. Introduction to Python comments Sometimes, you want to document the code that you write. For example, you may want to note why a piece of code [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-25","post","type-post","status-publish","format-standard","hentry","category-1-fundamentals"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/25","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=25"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/25\/revisions"}],"predecessor-version":[{"id":26,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/25\/revisions\/26"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=25"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=25"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=25"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}