Python Comment Block
Tags: python, programming, codenewbie, tutorial
Published: 2021-12-08 14:14:19

---
title: Python Comment Block
published: true
date: 2021-11-25 14:03:17 UTC
tags: #python #programming #codenewbie #tutorial
canonical_url: https://itsmycode.com/python-comment-block/
---

ItsMyCode |

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, multi-line comments, and documentation string.

## Introduction to [Python Comment](https://www.w3basic.com/python/python-comments/) Block

Comments are used to explain the source code. Comments are mainly used for the following purposes.

1. Improve Code readability
2. Testing the code
3. Explaining the code or Metadata of the project
4. Prevent execution of specific code blocks

For example, let’s 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.

Python interpreter ignores the comments while executing the code and only interprets the code.

## Types of comments in Python

There are three kinds of comments we can use in Python. 

1. Single-line comments
2. Multi-line comments
3. Documentation strings, aka docstrings

Let us look into details on how to use these comments in Python code with examples.

## Single-line comments

Single-line comments, also called block comments, start with a hash sign (#) followed by a single space and a text string.

The hash (#) works with only a single line of code and not on multi-line code. 

Let’s take an example to demonstrate single-line comments in Python.

```python 
# This is a single line comment example
print("Hello World")
```

## Inline comments

If you place the comment in the same line as a statement, you will have an inline comment.

Like single-line comments, inline comments also begin with a hash (#) sign and are followed by a space and the comment text. 

Let’s take an example to demonstrate inline comments in Python.

```python 
print("Hello World") # This is a example of inline comment
```

## Multi-line comments

Usually, in other languages like C, C#, Java, etc., we can [write](https://itsmycode.com/python-write-text-file/)a multi-line comment as shown below.

```python 
/* This is a comment block
which supports
Multi-line code */
```

But you cannot do this in Python.

Python does not have any built-in mechanism for commenting out multiple lines. However, there are different ways to achieve this in Python.

### Using Multiple Hashtags (#)

We can use multiple hashtags to write multi-line comments in Python. Each line that has a hash sign(#) is considered as a single line comment.

```python 
# This is how we can acheive 
# Multi-line comments in Python
print("Hello World")

```

## Python docstrings

Documentation strings, also called docstrings, are the [string literal](https://itsmycode.com/python-syntaxerror-eol-while-scanning-string-literal/) denoted with triple quotes that occur as the first statement in a module, function, class, or method definition.

**Note:**  We can also use triple “”” quotations to create docstrings.

### Single line docstrings

Let’s take an example to demonstrate single line docstring. ** **

```python 
def Add(a,b):
    '''Takes two number as input and returns sum of 2 numbers'''
    return a+b
```

Inside the triple quotation marks is the  **docstring**  of the function Add() as it appears right after its definition.

### Multi-line docstrings

The multi-line docstring can span across multiple lines of code starts with triple quotes(“””) and ends with triple quotes (“””).

The following example shows you how to use multi-line docstrings:

```python 
def Add(a,b):
    '''Takes two number as input 
     Adds a and b
     Returns sum of a and b as output
    '''
    return a+b

print(Add(5,6))
```

The post [Python Comment Block](https://itsmycode.com/python-comment-block/) appeared first on [ItsMyCode](https://itsmycode.com).