Technical Roadmap

Comments in Python

Comments are sticky notes - short explanations written by a programmer to describe what the code is doing.

These lines are not executed by the Python interpreter and have no effect on program execution

Where you can write comments:

  • within a block of code
  • at the end of a line
  • inside complex expressions to understand logic
The Logicoria Tip

Comments are like the side notes we write in our notebooks. They help explain the answers we’ve written.


Types of Comments in Python:

1. Single-Line Comments:

  • Use the hash symbol #
  • Anything written after # is ignored by Python
  • Can be written on a separate line or at the end of a line

Program to display single line comment

PYTHON
# This variable stores the total count of our articles
article_count = 24
print(article_count)

Output

24

Explanation

In the above program the line written after # was not executed by the interpreter therefore we got output as 24


2. Multi-Line Comments:

  • Python does not have a specific /* */ symbol
  • We use # on each line for multi-line comments
  • We can use triple quotes (""" or ''') for making comments but they are multi line strings, not actual comments.

a) Using # for Multi-Line Comments

PYTHON
# FILE: article_manager.py
# GOAL: To automate the upload of 100 articles.
# STATUS: Testing in Localhost (XAMPP).
print("Database logic initialized.")

Output

Database logic initialized.

b) Using Triple Quotes (" " " or ' ' ')

Triple quotes are actually Docstrings (Documentation String). When we place them inside a class or function, it associates that text with that object. You can use them as comments when u don't want to place # on multiple lines.

PYTHON
"""
FILE: article_manager.py
GOAL: To automate the upload of 100 articles.
STATUS: Testing in Localhost (XAMPP).
"""
print("Database logic initialized.")

Output

 Database logic initialized.

Quick Revision:

  • Explains code logic to humans.
  • Ignored by the Python interpreter.
  • Does not affect how the program runs.
  • Can be written anywhere in the code.
  • # = Single line comment.
  • Triple quotes = Best for multi-line documentation.

Related Articles