{"id":151,"date":"2024-10-15T03:34:17","date_gmt":"2024-10-15T03:34:17","guid":{"rendered":"https:\/\/itsmycode.com\/?p=151"},"modified":"2024-10-15T03:34:17","modified_gmt":"2024-10-15T03:34:17","slug":"python-comment-block","status":"publish","type":"post","link":"https:\/\/itsmycode.com\/python-comment-block\/","title":{"rendered":"Python Comment Block"},"content":{"rendered":"\n<p>Comments are a piece of text in a computer program that provides more information on the source code written. Like every other programming language, Python has three different types of comments: single-line comments, multiline comments, and documentation string to comment out block of code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-introduction-to-python-comment-block\">Introduction to Python Comment Block<\/h2>\n\n\n\n<p>Comments are used to explain the source code. Comments are mainly used for the following purposes.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Improve Code readability<\/li>\n\n\n\n<li>Testing the code<\/li>\n\n\n\n<li>Explaining the code or Metadata of the project<\/li>\n\n\n\n<li>Prevent execution of specific code blocks<\/li>\n<\/ol>\n\n\n\n<p>For example, let\u2019s say you have written complex business logic, formulas, algorithms, etc. Then we need to document it using the comments that explain what the code does, thus improving the readability of the code in Python.<\/p>\n\n\n\n<p>Python interpreter ignores the comments while executing the code and only interprets the code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-types-of-comments-in-python\">Types of comments in Python<\/h2>\n\n\n\n<p>There are three kinds of comments we can use in Python.&nbsp;<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Single-line comments<\/strong><\/li>\n\n\n\n<li><strong>Multiline comments<\/strong><\/li>\n\n\n\n<li><strong>Documentation strings, aka docstrings<\/strong><\/li>\n<\/ol>\n\n\n\n<p>Let us look into details on how to use these comments in Python code with examples.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-single-line-comments\">Single-line comments<\/h2>\n\n\n\n<p>Single-line comments, also called block comments, start with a hash sign (#) followed by a single space and a text string.<\/p>\n\n\n\n<p>The hash (#) works with only a single line of code and not on multi-line code.&nbsp;<\/p>\n\n\n\n<p>Let\u2019s take an example to demonstrate single-line comments in Python.<\/p>\n\n\n\n<p><strong>Example of Single-line comment in Python<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># This is a single line comment example\nprint(\"Hello World\")<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-inline-comments\">Inline comments<\/h2>\n\n\n\n<p>If you place the comment in the same line as a statement, you will have an inline comment.<\/p>\n\n\n\n<p>Like single-line comments, inline comments also begin with a hash (#) sign and are followed by a space and the comment text.&nbsp;<\/p>\n\n\n\n<p>Let\u2019s take an example to demonstrate inline comments in Python.<\/p>\n\n\n\n<p><strong>Python Inline comments<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(\"Hello World\") # This is a example of inline comment<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-multiline-comments\">Multiline comments<\/h2>\n\n\n\n<p>Usually, in other languages like <a href=\"https:\/\/www.w3basic.com\/golang\/\" target=\"_blank\" rel=\"noreferrer noopener\">Go<\/a>, C, C#, Java, etc., we can <a href=\"https:\/\/itsmycode.com\/python-write-text-file\/\" target=\"_blank\" rel=\"noreferrer noopener\">write <\/a>a multi-line comment as shown below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/* This is a comment block\nwhich supports\nMulti-line code *\/<\/code><\/pre>\n\n\n\n<p>But in Python Multiline comments do not exist built-in like other programming languages.<\/p>\n\n\n\n<p>Python may not have any built-in mechanism for commenting out multiple lines. However, there are different ways to achieve this in Python.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-using-multiple-hashtags\">Using Multiple Hashtags (#)<\/h3>\n\n\n\n<p>We can use multiple hashtags to write multiline comments in Python. Each line that has a hash sign(#) is considered as a single-line comment.<\/p>\n\n\n\n<p><strong>Multi-line comments Example<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># This is how we can acheive \n# Multi-line comments in Python\nprint(\"Hello World\")<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-python-docstrings\">Python docstrings<\/h2>\n\n\n\n<p>Documentation strings, also called docstrings, are the <a href=\"https:\/\/itsmycode.com\/python-syntaxerror-eol-while-scanning-string-literal\/\" target=\"_blank\" rel=\"noreferrer noopener\">string literal<\/a> denoted with triple quotes that occur as the first statement in a module, function, class, or method definition. Conventions for writing good documentation strings (a.k.a. \u201cdocstrings\u201d) are immortalized in&nbsp;<a href=\"https:\/\/www.python.org\/dev\/peps\/pep-0257\" target=\"_blank\" rel=\"noreferrer noopener\">P<\/a><a href=\"https:\/\/www.python.org\/dev\/peps\/pep-0257\" target=\"_blank\" rel=\"noreferrer noopener\">E<\/a><a href=\"https:\/\/www.python.org\/dev\/peps\/pep-0257\" target=\"_blank\" rel=\"noreferrer noopener\">P 257<\/a>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Note:<\/strong>&nbsp;We can also use triple <strong><code>\"\"\"<\/code><\/strong> quotations to create docstrings.<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-single-line-docstrings\">Single line docstrings<\/h3>\n\n\n\n<p>Let\u2019s take an example to demonstrate single line docstring.<\/p>\n\n\n\n<p><strong>Python Docstrings Example<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def Add(a,b):\n    '''Takes two number as input and returns sum of 2 numbers'''\n    return a+b<\/code><\/pre>\n\n\n\n<p>Inside the triple quotation marks is the&nbsp;<strong>docstring<\/strong>&nbsp;of the function Add() as it appears right after its definition.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-multi-line-docstrings\">Multi-line docstrings<\/h3>\n\n\n\n<p>The multi-line docstring can span across multiple lines of code starts with triple quotes(<strong><code>\"\"\"<\/code><\/strong>) and ends with triple quotes (<strong><code>\"\"\"<\/code><\/strong>).<\/p>\n\n\n\n<p>We can use multiline docstring as multiline comments in Python to comment out block of code. The creator of Python, Guido Van Rossum, also recommended this and mentioned it on Twitter as a Pro-tip.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><a href=\"https:\/\/twitter.com\/BSUCSClub?ref_src=twsrc%5Etfw\" target=\"_blank\" rel=\"noreferrer noopener\">@BSUCSClub<\/a> Python tip: You can use multi-line strings as multi-line comments. Unless used as docstrings, they generate no code! \ud83d\ude42\u2014 Guido van Rossum (@gvanrossum) <a href=\"https:\/\/twitter.com\/gvanrossum\/status\/112670605505077248?ref_src=twsrc%5Etfw\" target=\"_blank\" rel=\"noreferrer noopener\">September 10, 2011<\/a><\/p>\n<\/blockquote>\n\n\n\n<p>The following example shows you how to use multi-line docstrings. Make sure to indent the leading&nbsp;<code>'''<\/code>&nbsp;appropriately to avoid an&nbsp;<code><a href=\"https:\/\/itsmycode.com\/python-indentationerror-unindent-does-not-match-any-outer-indentation-level-solution\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>IndentationError<\/strong><\/a><\/code><\/p>\n\n\n\n<p><strong>Multiline docstring example<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def Add(a,b):\n    '''Takes two number as input \n     Adds a and b\n     Returns sum of a and b as output\n    '''\n    return a+b\n\nprint(Add(5,6))<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Note:<\/strong> As long as the string is not assigned to any Python variable, Python will read the code but then ignore it, and you have made a multiline comment.<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Comments are a piece of text in a computer program that provides more information on the source code written. Like every other programming language, Python has three different types of comments: single-line comments, multiline comments, and documentation string to comment out block of code. Introduction to Python Comment Block Comments are used to explain the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":152,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,1],"tags":[],"class_list":["post-151","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-basics","category-python"],"_links":{"self":[{"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/posts\/151","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/comments?post=151"}],"version-history":[{"count":1,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/posts\/151\/revisions"}],"predecessor-version":[{"id":153,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/posts\/151\/revisions\/153"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/media\/152"}],"wp:attachment":[{"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/media?parent=151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/categories?post=151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/tags?post=151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}