⨠AI-generated content. This page was written with the assistance of an
AI language model and is provided as a learning aid. Despite careful
review, it may still contain mistakes, omissions, or out-of-date
information. Whether you are new to the topic, a team lead, or a senior
practitioner, treat it as a starting point rather than an authoritative
reference: read it critically and independently verify anything you act on
(code, commands, figures, and factual claims) against official
documentation and primary sources before relying on it.
Code expresses what it does; documentation explains why and how, and thinking in
algorithms shapes code before it is written. This lesson covers comments,
docstrings, and algorithmic thinking ā the practices that make Python code
understandable and well-planned, completing the basics stage.
A comment is text in code that Python ignores, written for human readers. In Python,
comments start with #:
# Convert the raw price strings to numbers before summingprices=[float(p)forpinraw_prices]total=sum(prices)# sum() adds all elements
Good comments explain why ā the reasoning, the intent, the non-obvious ā not what the
code plainly does. x=x+1#addonetox is a useless comment (the code says
that); #compensateforthezero-basedindex is a useful one (it explains the reason).
The documentation principle from the foundations ā explain why, not just what ā applies
directly.
A docstring is a string at the start of a function (or module or class) that
documents it, accessible programmatically and by tools. Python data work commonly uses
the NumPy documentation style, which structures a docstring into sections:
defadd_tax(amount,rate=0.08):"""Return the amount with tax added. Parameters ---------- amount : float The pre-tax amount. rate : float, optional The tax rate as a decimal (default 0.08). Returns ------- float The amount including tax. """returnamount+amount*rate
The docstring states what the function does, its Parameters, and what it
Returns ā so a reader (or a documentation generator) understands the function without
reading its body. For reusable functions, a docstring is the interfaceās documentation,
and the NumPyDoc section order (Parameters, Returns, and, as needed, Raises, Notes,
Examples) is a widely-followed convention in the data ecosystem.
An algorithm is a step-by-step procedure for solving a problem ā and thinking
algorithmically means planning the steps before writing code. Before coding, an analyst
works out the logic: what steps, in what order, transform the input into the desired
output. Writing the algorithm first (even as plain-language steps or āpseudocodeā) clarifies
the approach before the syntax, catching logic problems early ā the big-picture-first
discipline from the foundations, applied to code. Code is the expression of an algorithm;
getting the algorithm right first makes the code straightforward.
Documentation is what makes code understandable to its future readers ā including its
author months later. A comment explaining a non-obvious choice, a docstring describing a
functionās interface, and code that follows a clear algorithm together mean the reasoning
survives, not just the instructions. This is the reproducibility-and-maintainability
theme in code: undocumented code works until it must be understood or changed, at which
point its opacity becomes costly. Documentation is the small investment that keeps code
workable over time.
Documentation has the same failure modes as any: too little leaves code opaque, but too
much ā comments restating obvious code, docstrings on trivial one-line functions,
narration of every step ā clutters and, worse, drifts out of sync with the code, so a
comment says one thing while the code does another, which misleads more than no comment.
The discipline is documentation that is accurate, useful, and maintained: comment the
why and the non-obvious, docstring the interfaces that will be reused, keep it truthful
to the code, and skip narrating what the code plainly says. Accurate, purposeful
documentation helps; stale or redundant documentation harms. This completes the Python
basics; the next stage turns to control flow ā making code decide and repeat.
Comments, Algorithms, and Docstrings in Python#
š Data Analysis Using Python š Python Fundamentals Lesson 010
ā Previous Ā· Next ā¶ Ā· ā Section Ā· ā Hub
Important
⨠AI-generated content. This page was written with the assistance of an AI language model and is provided as a learning aid. Despite careful review, it may still contain mistakes, omissions, or out-of-date information. Whether you are new to the topic, a team lead, or a senior practitioner, treat it as a starting point rather than an authoritative reference: read it critically and independently verify anything you act on (code, commands, figures, and factual claims) against official documentation and primary sources before relying on it.
Documenting code#
Code expresses what it does; documentation explains why and how, and thinking in algorithms shapes code before it is written. This lesson covers comments, docstrings, and algorithmic thinking ā the practices that make Python code understandable and well-planned, completing the basics stage.
Comments#
A comment is text in code that Python ignores, written for human readers. In Python, comments start with
#:Good comments explain why ā the reasoning, the intent, the non-obvious ā not what the code plainly does.
x = x + 1 # add one to xis a useless comment (the code says that);# compensate for the zero-based indexis a useful one (it explains the reason). The documentation principle from the foundations ā explain why, not just what ā applies directly.Docstrings#
A docstring is a string at the start of a function (or module or class) that documents it, accessible programmatically and by tools. Python data work commonly uses the NumPy documentation style, which structures a docstring into sections:
The docstring states what the function does, its Parameters, and what it Returns ā so a reader (or a documentation generator) understands the function without reading its body. For reusable functions, a docstring is the interfaceās documentation, and the NumPyDoc section order (Parameters, Returns, and, as needed, Raises, Notes, Examples) is a widely-followed convention in the data ecosystem.
Thinking in algorithms#
An algorithm is a step-by-step procedure for solving a problem ā and thinking algorithmically means planning the steps before writing code. Before coding, an analyst works out the logic: what steps, in what order, transform the input into the desired output. Writing the algorithm first (even as plain-language steps or āpseudocodeā) clarifies the approach before the syntax, catching logic problems early ā the big-picture-first discipline from the foundations, applied to code. Code is the expression of an algorithm; getting the algorithm right first makes the code straightforward.
Why documentation matters#
Documentation is what makes code understandable to its future readers ā including its author months later. A comment explaining a non-obvious choice, a docstring describing a functionās interface, and code that follows a clear algorithm together mean the reasoning survives, not just the instructions. This is the reproducibility-and-maintainability theme in code: undocumented code works until it must be understood or changed, at which point its opacity becomes costly. Documentation is the small investment that keeps code workable over time.
The caveat#
Documentation has the same failure modes as any: too little leaves code opaque, but too much ā comments restating obvious code, docstrings on trivial one-line functions, narration of every step ā clutters and, worse, drifts out of sync with the code, so a comment says one thing while the code does another, which misleads more than no comment. The discipline is documentation that is accurate, useful, and maintained: comment the why and the non-obvious, docstring the interfaces that will be reused, keep it truthful to the code, and skip narrating what the code plainly says. Accurate, purposeful documentation helps; stale or redundant documentation harms. This completes the Python basics; the next stage turns to control flow ā making code decide and repeat.
Hint
Code Reusability, Modularity, and Clean Code in Python
Functions in Python
Branching and Conditional Statements in Python
Naming Conventions and Restrictions in Python
See also
Source article Adapted (context, re-expressed) in our own words from: https://insightful-data-lab.com/2023/12/06/comments-algorithms-and-docstrings-in-python/ (insightful-data-lab.com).
Tags: purpose: reference topic: data analytics topic: python topic: basics