Python Comments

As your Python programs become larger, they also become harder to understand. Imagine you write a program today. A week later, you open it again and wonder:

"Why did I write this code?"

Or maybe another programmer needs to read your program. Without any explanation, understanding your code can take time.

This is where comments become useful.

What are Python Comments?

Python comments are non-executable lines of text within a program that are ignored by the interpreter. Comments are written to help people understand the code. They do not affect how the program works.

The purpose of Python comments is:

  • Explain what a piece of code does.
  • Make the code easier for themselves and others to understand.
  • Leave reminders or notes for future changes.
  • Make debugging easier by explaining the purpose of the code.

Types of Python Comments

There are four common ways to write comments in Python:

  1. Single-line comments
  2. Inline comments
  3. Multi-line comments
  4. Triple-quoted strings

1. Single-line Comments

A single-line comment starts with the hash symbol (#). Everything after the # on that line is ignored by Python.

2. Inline Comments

An inline comment is simply a single-line comment written after a line of code.

Inline comments are useful when you want to briefly explain a specific line of code.

3. Multi-line Comments

Python does not have a special syntax for multi-line comments. Instead, programmers usually write multiple single-line comments.

# This is the first line of a comment.
# This is the second line.
# This is the third line.

4. Triple-quoted Strings

Sometimes you may see programmers use triple quotes (""" """ or ''' ''') to write longer notes or comments.

Although this looks like a multi-line comment, it is actually a string.

Since the string is not assigned to a variable or used anywhere, Python simply ignores it while running the program.

If you assign a triple-quoted string to a variable, it becomes a normal string. It is no longer ignored by Python and can be used or printed like any other string.

Output

This is a message
stored inside the variable 'text'.

Notice that the text is printed because it is stored in the variable text.