{"id":75,"date":"2020-10-03T01:13:06","date_gmt":"2020-10-03T01:13:06","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=75"},"modified":"2025-03-31T01:50:14","modified_gmt":"2025-03-31T01:50:14","slug":"python-comments","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-basics\/python-comments\/","title":{"rendered":"Python Comments"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to add comments to your code. And you&#8217;ll learn various kinds of Python comments including block comments, inline comments, and documentation string.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-python-comments'>Introduction to Python comments <a href=\"#introduction-to-python-comments\" class=\"anchor\" id=\"introduction-to-python-comments\" title=\"Anchor for Introduction to Python comments\">#<\/a><\/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\" id='python-block-comments'>Python block comments <a href=\"#python-block-comments\" class=\"anchor\" id=\"python-block-comments\" title=\"Anchor for Python block comments\">#<\/a><\/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<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\"># increase price by 5%<\/span>\nprice = price * <span class=\"hljs-number\">1.05<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=cHJpY2UgPSAxMDsKCiMgaW5jcmVhc2UgcHJpY2UgYnkgNSUKcHJpY2UgPSBwcmljZSAqIDEuMDUKCiMgZGlzcGxheSB0aGUgcHJpY2UKcHJpbnQocHJpY2Up\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='python-inline-comments'>Python inline comments <a href=\"#python-inline-comments\" class=\"anchor\" id=\"python-inline-comments\" title=\"Anchor for Python inline comments\">#<\/a><\/h2>\n\n\n\n<p>When you place a comment on the same line as a statement, you&#8217;ll 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<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">salary = <span class=\"hljs-number\">120000<\/span>\nsalary = salary * <span class=\"hljs-number\">1.02<\/span>   <span class=\"hljs-comment\"># increase salary by 2%<\/span>\n\nprint(salary)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=c2FsYXJ5ID0gMTIwMDAwCnNhbGFyeSA9IHNhbGFyeSAqIDEuMDIgICAjIGluY3JlYXNlIHNhbGFyeSBieSAyJQoKcHJpbnQoc2FsYXJ5KQ%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\">122400<span class=\"hljs-selector-class\">.0<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='python-docstrings'>Python docstrings <a href=\"#python-docstrings\" class=\"anchor\" id=\"python-docstrings\" title=\"Anchor for Python docstrings\">#<\/a><\/h2>\n\n\n\n<p>A documentation string is a <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-string\/\">string<\/a> literal that you put as the first lines in a code block, for example, a <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-functions\/\">function<\/a>.<\/p>\n\n\n\n<p>Unlike a regular comment, a documentation string can be accessed at run-time using&nbsp; <code>obj.__doc__<\/code> attribute where <code>obj <\/code>is the name of the <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-functions\/\">function<\/a>.<\/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&#8217;re 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\" id='one-line-docstrings'>One-line docstrings <a href=\"#one-line-docstrings\" class=\"anchor\" id=\"one-line-docstrings\" title=\"Anchor for One-line docstrings\">#<\/a><\/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&#8217;t 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&nbsp;docstring in&nbsp;the <code>quicksort()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">quicksort<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n<span class=\"hljs-string\">\"\"\" sort the list using quicksort algorithm \"\"\"<\/span>\n...<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='multi-line-docstrings'>Multi-line docstrings <a href=\"#multi-line-docstrings\" class=\"anchor\" id=\"multi-line-docstrings\" title=\"Anchor for Multi-line docstrings\">#<\/a><\/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<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">increase<\/span><span class=\"hljs-params\">(salary, percentage, rating)<\/span>:<\/span>\n    <span class=\"hljs-string\">\"\"\" increase salary base on rating and percentage\n    rating 1 - 2 no increase\n    rating 3 - 4 increase 5%\n    rating 4 - 6 increase 10%\n    \"\"\"<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='python-multiline-comments'>Python multiline comments <a href=\"#python-multiline-comments\" class=\"anchor\" id=\"python-multiline-comments\" title=\"Anchor for Python multiline comments\">#<\/a><\/h2>\n\n\n\n<p>Python doesn&#8217;t support multiline comments. <\/p>\n\n\n\n<p>However, you can use multi-line docstrings as multiline comments. <a rel=\"noreferrer noopener\" href=\"https:\/\/twitter.com\/gvanrossum\/status\/112670605505077248\" target=\"_blank\">Guido van Rossum<\/a>, the creator of Python, also recommended this.<\/p>\n\n\n\n<p>It&#8217;s 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\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use comments to document your code when necessary.<\/li>\n\n\n\n<li>A block comment and inline comment starts with a hash sign (<code>#<\/code>).<\/li>\n\n\n\n<li>Use docstrings for <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-functions\/\">functions<\/a>,&nbsp;<a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-module\/\">modules<\/a>, and <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-class\/\">classes<\/a>.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=comments\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"75\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-comments\/\"\n\t\t\t\tdata-post-title=\"Python Comments\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"75\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-comments\/\"\n\t\t\t\tdata-post-title=\"Python Comments\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you&#8217;ll learn how to add comments to your code. And you&#8217;ll 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":37,"menu_order":6,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-75","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/75","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=75"}],"version-history":[{"count":1,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/75\/revisions"}],"predecessor-version":[{"id":7267,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/75\/revisions\/7267"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/37"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=75"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}