Python Comments Complete Tutorial

  • Post author:
  • Post category:Python

In this section, we will learn what comments are and how to use them in Python.

What are comments in Python?

Comments are a way of describing the instructions in a source code to other developers.

Basically, comments are for programmers and not computers! For computers comments will be ignored.

We use comments specially when the source code of a program gets lengthy and developers want to know how the instructions are put together and basically how the algorithm is written for the program so that they know where and how to modify the source code in order to either fix bugs or advance the program.

How to comment in Python? Types of Comments in Python

In short, there are two types of comments in Python:

  1. Single Line Comment
  2. Multi Line Comment

Single Line Comment in Python

As the name suggests, a single line comment is used when we want to put a comment on a source code that is short in the number of characters! Basically, when a comment does not take more than one line, we use the single line comment.

Python Single Line Comment Syntax:

# comments…

Start with a Hash tag and then put the comment.

Notes:

  • Anything that comes after the hash tag is part of the comment and Python interpreter will simply skip over it (ignores it).
  • A single line of comment is only for one line! That means if our comment takes at least two or more lines, then we need to use a hash tag for each line.

For example:

# comment line 1…

# comment line 2…

Example: creating single line comment in Python

# This is a function and you'll learn about it in later section
# Also the function is super simple and I can't wait to teach you about it
def functionOne():
    print("Hello from functionOne")

functionOne()

Output:

Hello from functionOne

In this example, there are two single line comments on top of the `functionOne`.

Again, python interpreter skips any comment in Python.

Multi Line Comment in Python

When we have a lengthy comment (perhaps explaining an algorithm inside a source code) and it takes multiple lines, we can use the multi-line comment.

A multi-line comment, as the name suggests, it allows us to write a comment that takes multiple lines.

Python Multi-Line Comment Syntax:

"""

Put the comments here…

"""

A multi-line comment starts with three pairs of double-quotations. After that comes the comments.

At the end, we put another three pairs of double-quotations to close the comment area.

Example: creating multi line comment in Python

"""

This is a

multi-line

comment...

"""

def functionOne():

               # A single line of comment inside the body of the functionOne

               print("Hello from functionOne")




functionOne()

Output:

Hello from functionOne

Where to use comments in Python?

  • Comments can be used in empty spaces in a program. For example, before a function, in order to explain the instructions within that function.
  • On top of a source code in order to explain the overall work of that source code…
  • Within the body of a function.
  • On top or below a variable.

And other places that do not break the source code itself! For example, we can’t use comments between the name of a variable or functions, etc.

Leave a Reply