Free ATS Friendly Resume Builder Online

Create Your Resume

Resume Builder

Resume Maker

Resume Templates

Resume PDF Download

Create Your Resume is a free online resume builder that helps job seekers create professional, ATS friendly resumes in minutes. Easily build, customize, and download modern resume templates in PDF format.

Our resume maker is designed for freshers and experienced professionals looking to create job-ready resumes. Choose from multiple resume templates, customize sections, and generate ATS optimized resumes online for free.

Create resumes for IT jobs, software developers, freshers, experienced professionals, managers, and students. This free resume builder supports CV creation, resume PDF download, and online resume editing without signup.

Back to Python
Lesson 3 of 17

What Are Comments and Documentation Strings in Python?

Comments and documentation strings (docstrings) in Python are tools that help humans understand code without affecting how the program runs. While Python code tells the computer what to do, comments and docstrings explain why the code exists and how it should be used. Comments are used for short explanations, reminders, or temporarily disabling code. Docstrings go deeper—they formally document modules, functions, classes, and methods so other developers (and tools) can understand their purpose and usage. Python treats docstrings as part of the code structure, making them accessible through built-in help systems and documentation generators. This topic is essential because readable code is more valuable than clever code. In real-world development, code is read far more often than it is written. Mastering comments and docstrings early builds professional habits, improves collaboration, simplifies debugging, and prepares you to write production-quality Python software.

Why Comments and Docstrings Matter in Python

Python emphasizes readability, but even clean code cannot always explain intent, decisions, or usage. Comments and docstrings fill this gap.

They exist for humans—not for the Python interpreter— and are critical for maintainability and collaboration.

What Are Comments in Python?

Comments are notes written inside code that Python completely ignores during execution. They are used to explain logic, clarify intent, or temporarily disable code.

Single-Line Comments

Python uses the # symbol for single-line comments. Everything after it on the same line is ignored.

# This is a single-line comment
x = 10  # Assigning value to x

When to Use Single-Line Comments

  • Explaining non-obvious logic
  • Clarifying complex conditions
  • Adding context for future readers

Multi-Line Comments in Python

Python does not have a true multi-line comment syntax. Instead, developers use multiple single-line comments.

# This function calculates the final price
# after applying discount and tax
# based on business rules

This approach keeps comments explicit and readable.

Commenting Out Code

Comments are often used to temporarily disable code during testing or debugging.

# print("Debug message")
# result = process_data(data)

This is useful for experimentation but should not replace proper debugging techniques in production code.

Best Practices for Writing Comments

  • Explain why, not what the code obviously does
  • Keep comments concise and meaningful
  • Avoid outdated or misleading comments
  • Let clean code reduce the need for comments

What Are Documentation Strings (Docstrings)?

Docstrings are special string literals used to document modules, functions, classes, and methods. Unlike comments, docstrings are stored as metadata and can be accessed at runtime.

They are written using triple quotes: ''' or """.

Function Docstring Example

def add(a, b):
    """
    Adds two numbers and returns the result.

    Parameters:
    a (int): First number
    b (int): Second number

    Returns:
    int: Sum of a and b
    """
    return a + b

How Docstrings Are Used

Docstrings serve multiple purposes:

  • Explain what a function or class does
  • Describe parameters and return values
  • Support built-in help systems
  • Enable automatic documentation tools

Accessing Docstrings

print(add.__doc__)
help(add)

This makes Python self-documenting.

Module-Level Docstrings

A module docstring appears at the top of a Python file and describes the purpose of the entire module.

"""
This module handles user authentication
and session management.
"""

Class Docstrings

Class docstrings describe the purpose and behavior of a class and its responsibilities.

class User:
    """
    Represents a user in the system.
    Stores user details and authentication status.
    """

Comments vs Docstrings

Aspect Comments Docstrings
Used for Internal explanation Formal documentation
Accessible at runtime No Yes
Syntax # ''' or """
Best for Developers reading code Users and tools

Common Mistakes Beginners Make

  • Using comments instead of clear variable names
  • Writing comments that repeat obvious code
  • Using docstrings as regular comments
  • Skipping documentation entirely

Real-World Example

In a team project, a well-written docstring allows a new developer to use a function correctly without reading its implementation. This reduces onboarding time and prevents misuse.

Performance and Security Considerations

Comments do not affect performance. Docstrings add negligible memory overhead and are safe to use.

However, sensitive information should never be placed in comments or docstrings, as source code is often shared.

Summary

Comments and documentation strings are essential tools for writing professional Python code. Comments explain intent and logic, while docstrings formally document how code should be used. Together, they improve readability, collaboration, debugging, and long-term maintainability. Mastering them early transforms you from someone who writes code into someone who writes code others can trust and reuse.