Codectionary / Developer documentation / Python

Comments & Docstrings

Comments let you annotate code with explanations that Python ignores at runtime. A single-line comment starts with #. Triple-quoted strings (''' or \"\"\") are technically string literals, but when placed as the first statement in a module, function, or class, they become docstrings - a convention used by documentation tools and the built-in help() function.

Syntax

# single-line comment\n"""docstring"""

Examples

Single-Line Comments

Explaining a line or block of code with #.

# Calculate the area of a circle
radius = 5
area = 3.14159 * radius ** 2  # pi * r^2
print(area)

Function Docstrings

Documenting what a function does, its parameters, and its return value.

def add(a, b):
    """
    Add two numbers together.

    Args:
        a (int): The first number.
        b (int): The second number.

    Returns:
        int: The sum of a and b.
    """
    return a + b

print(add.__doc__)

Commenting Out Code

Temporarily disabling a line of code while debugging.

total = 0
for i in range(10):
    total += i
    # print(f"Running total: {total}")  # disabled for now

print(total)

Best practices

  • Write comments that explain *why* the code does something, not *what* it does - the code itself should already show what
  • Add a docstring to every public function, class, and module so help() and documentation tools can describe them
  • Keep comments up to date - a comment that contradicts the code it describes is worse than no comment at all
  • Use triple-quoted strings for docstrings even on a single line ('''One-line summary.''') for consistency

At a glance

Purpose
Scripting and general-purpose applications
File extension
.py
Runs in
Python interpreter
Usually used with
Python standard library and packages

Specifications & further reading

Related Python documentation

True
In Python, True is a built-in constant that represents the boolean value of true. Alongside False, True enables logical and conditional operations. It's a fundamental keyword used throughout Python programming for decision-making, loops, comparisons, and boolean logic. True plays a key role in if statements, while loops, and logical expressions.
False
False is Python's built-in constant representing the boolean value false. It's the logical opposite of True and is essential for conditional logic, loop control, and boolean operations. False is used to indicate negative conditions, failed validations, or when something is not the case. Understanding False is crucial for writing effective control flow in Python programs.
None
None is Python's built-in singleton value that represents the absence of a value or a null result. It is its own type (NoneType) and is commonly used as a default parameter value, a placeholder before a variable is assigned real data, or a function's implicit return value when nothing is explicitly returned. Unlike 0, an empty string, or an empty list, None specifically means 'no value here', not 'an empty value'.
Variables & Assignment
Variables in Python are names bound to values, created the moment you first assign to them - there's no need to declare a type in advance, since Python is dynamically typed. A single variable can be reassigned to a completely different type later in the program. Python also supports several convenient assignment patterns, including assigning multiple variables at once and using augmented assignment operators.