Python Comments

Comments in Python are used to explain code, improve readability, and make programs easier to understand. They are ignored by the Python interpreter and do not affect program execution.
Comments are an essential part of writing clean, professional, and maintainable code.

What Are Comments in Python?

Comments are notes written inside the code to:
  • Describe what the code does
  • Explain complex logic
  • Help other developers understand the program
  • Remind yourself why certain decisions were made
Definition

A comment is a piece of text in a program that is not executed by Python.


Why Comments Are Important

Writing comments helps in:
  • Code readability
  • Debugging and maintenance
  • Team collaboration
  • Learning and teaching programming
Well-commented code is easier to:
  • Modify
  • Review
  • Reuse
Content Creator Tip

Good comments explain why something is done, not just what is done.


Single-Line Comments

Single-line comments are the most common type of comments in Python. They start with the # symbol.
Anything written after # on that line is ignored by Python.

Code Example 1: Basic Single-Line Comment

python
# This is a single-line comment
print("Hello Python")

Code Example 2: Comment Before Code

python
# Store user age
age = 25
print(age)

Code Example 3: Inline Comment

python
x = 10  # x stores a number
print(x)

Multi-Line Comments

Python does not have a dedicated syntax for multi-line comments. However, triple-quoted strings are commonly used as multi-line comments.
These are often used to:
  • Explain large sections of code
  • Add documentation
  • Describe logic in detail

Code Example 1: Multi-Line Comment Using Triple Quotes

python
"""
This program prints a welcome message.
It demonstrates the use of multi-line comments.
"""
print("Welcome")

Code Example 2: Multi-Line Description

python
'''
This block explains the logic of the program
Step 1: Take input
Step 2: Process data
Step 3: Display output
'''
print("Process Complete")

Code Example 3: Multi-Line Comment Inside Code

python
x = 10
"""
The variable x stores an integer value
which will be used later in calculations
"""
print(x)
Important

Triple-quoted strings are mainly used for documentation, not true comments.


Docstrings (Documentation Strings)

Docstrings are special comments used to document functions, classes, and modules. They are written using triple quotes and can be accessed at runtime.
Docstrings help tools and developers understand how code works.

Code Example 1: Function Docstring

python
def add(a, b):
    """
    This function returns the sum of two numbers.
    """
    return a + b

Code Example 2: Class Docstring

python
class Student:
    """
    This class represents a student.
    """
    pass

Code Example 3: Accessing Docstring

python
print(add.__doc__)
Best Practice

Always use docstrings for functions and classes.


Commenting Best Practices

Follow these rules while writing comments:
  • Keep comments clear and short
  • Avoid obvious comments
  • Update comments when code changes
  • Use proper grammar and spelling

Bad Comment Example

python
x = x + 1  # increment x by 1

Good Comment Example

python
# Increase retry count after failure
retries = retries + 1

Common Mistakes with Comments

  • Over-commenting simple code
  • Writing outdated comments
  • Using comments instead of clear variable names
Caution

Wrong or outdated comments can be more harmful than no comments.


Why Comments Matter in Real Projects

In real-world projects:
  • Code is read more than written
  • Teams depend on comments for clarity
  • Good comments reduce onboarding time
Learning Insight

Clean code with good comments is a sign of a professional developer.


Summary

  • Comments explain and document code
  • Python supports single-line and multi-line comments
  • Docstrings are used for documentation
  • Comments improve readability and maintenance
  • Best practices make comments effective

Exercise

  • Add comments to explain a small program
  • Write a docstring for a function
  • Identify and remove unnecessary comments