The Algorithms - Python Python TheAlgorithms/Python

Getting Started with Your First Contribution to TheAlgorithms/Python

Walk the README disclaimer, the CONTRIBUTING.md algorithm requirements, the canonical insertion_sort file shape, the auto-generated DIRECTORY.md, the pytest --doctest-modules gate, and the path to deeper algorithm tours.

6 stops ~16 min Verified 2026-05-05
What you will learn
  • How the README frames the project as an educational reference, not a production library
  • What the contribution checklist requires of every algorithm: type hints, docstrings with URL, doctests for valid and invalid input
  • How sorts/insertion_sort.py demonstrates the canonical file shape every contributor copies
  • Why DIRECTORY.md is auto-generated by CI and must never be hand-edited
  • How pyproject.toml configures pytest --doctest-modules so every docstring example becomes a CI test
  • Where to take your first PR and how the per-directory snake_case rule keeps placement obvious
Prerequisites
  • Python 3.14 installed locally
  • Comfort reading short Python files with type hints
  • Familiarity with git and GitHub pull requests
1 / 6

README: The Mission and the Disclaimer

README.md:36

The README is one screen. It declares the educational mission and immediately disclaims that these implementations are slower than the standard library.

A first contributor needs the project's stance before they pick a file to edit. The tagline says All algorithms implemented in Python - for education, and the disclaimer immediately under it is unambiguous: "Implementations are for learning purposes only. They may be less efficient than the implementations in the Python standard library." That sentence sets the bar for every PR. Optimization for production performance is not the goal; clarity, type-hint coverage, and a working doctest are. The Getting Started section sends the reader straight to CONTRIBUTING.md, which is where the actual contribution checklist lives. The README itself contains no install steps, no API reference, and no tutorial because the repository is not packaged for installation.

Key takeaway

The README is short on purpose. The mission is education, the disclaimer ships pedagogy ahead of performance, and contribution rules live in CONTRIBUTING.md.

<!-- Short description: -->
  <h3>All algorithms implemented in Python - for education 📚</h3>
</div>

Implementations are for learning purposes only. They may be less efficient than the implementations in the Python standard library. Use them at your discretion.

## 🚀 Getting Started

📋 Read through our [Contribution Guidelines](CONTRIBUTING.md) before you contribute.
2 / 6

CONTRIBUTING.md: What "an Algorithm" Means Here

CONTRIBUTING.md:41

Every merged file must satisfy a checklist: type hints, docstrings with URL references, doctests for valid and invalid input, no plotting, no printing.

The repository accepts a narrow definition of "algorithm," which is what gives the catalog its consistency. The five-bullet checklist near the bottom of this section is the working spec. Every merged file has type hints on its parameters and return value, raises a Python exception on bad input, ships a docstring that explains the algorithm and links to the source paper or Wikipedia article, and carries doctests that exercise both valid and invalid inputs. The function returns its result instead of printing it, so the algorithm composes into other code. The last paragraph rules out wrapper code: a contribution that calls a stdlib function adds no value and gets rejected. New algorithms only.

Key takeaway

If your file lacks type hints, a sourced docstring, or doctests for valid and invalid inputs, the PR will not pass review. Wrapping a stdlib call is not a contribution.

#### What is an Algorithm?

An Algorithm is one or more functions (or classes) that:
* take one or more inputs,
* perform some internal calculations or data manipulations,
* return one or more outputs,
* have minimal side effects (Ex. `print()`, `plot()`, `read()`, `write()`).

Algorithms should be packaged in a way that would make it easy for readers to put them into larger programs.

Algorithms should:
* have intuitive class and function names that make their purpose clear to readers
* use Python naming conventions and intuitive variable names to ease comprehension
* be flexible to take different input values
* have Python type hints for their input parameters and return values
* raise Python exceptions (`ValueError`, etc.) on erroneous input values
* have docstrings with clear explanations and/or URLs to source materials
* contain doctests that test both valid and erroneous input values
* return all calculation results instead of printing or plotting them

Algorithms in this repo should not be how-to examples for existing Python packages. Instead, they should perform internal calculations or manipulations to convert input values into different output values. Those calculations or manipulations can use data types, classes, or functions of existing Python packages but each algorithm in this repo should add unique value.
3 / 6

sorts/insertion_sort.py: The Canonical File Shape

sorts/insertion_sort.py:1

One algorithm, one file. Module docstring with run instructions, type-hinted signature, doctests for several input shapes, then the implementation.

This file is what the contribution checklist asks for, made concrete. The module docstring at the top explains the algorithm and tells you exactly how to run the doctests (python3 -m doctest -v insertion_sort.py). The signature uses Python 3.13+ type-parameter syntax (def insertion_sort[T: Comparable]) bound to a Comparable Protocol so the function works on any mutable sequence of comparable items. The function docstring carries six doctest examples covering the empty list, negative numbers, strings, and 100-element random samples. Each is a real test pytest runs on every push. The implementation itself is six lines at the bottom. A new sort drops into sorts/ with this exact shape.

Key takeaway

Copy this file's structure: module docstring with run command, typed signature, function docstring with doctests for several input shapes, then the implementation. The doctests are tests.

"""
A pure Python implementation of the insertion sort algorithm

This algorithm sorts a collection by comparing adjacent elements.
When it finds that order is not respected, it moves the element compared
backward until the order is correct.  It then goes back directly to the
element's initial position resuming forward comparison.

For doctests run following command:
python3 -m doctest -v insertion_sort.py

For manual testing run:
python3 insertion_sort.py
"""

from collections.abc import MutableSequence
from typing import Any, Protocol, TypeVar


class Comparable(Protocol):
    def __lt__(self, other: Any, /) -> bool: ...


T = TypeVar("T", bound=Comparable)


def insertion_sort[T: Comparable](collection: MutableSequence[T]) -> MutableSequence[T]:
    """A pure Python implementation of the insertion sort algorithm

    :param collection: some mutable ordered collection with heterogeneous
    comparable items inside
    :return: the same collection ordered by ascending

    Examples:
    >>> insertion_sort([0, 5, 3, 2, 2])
    [0, 2, 2, 3, 5]
    >>> insertion_sort([]) == sorted([])
    True
    >>> insertion_sort([-2, -5, -45]) == sorted([-2, -5, -45])
    True
    >>> insertion_sort(['d', 'a', 'b', 'e', 'c']) == sorted(['d', 'a', 'b', 'e', 'c'])
    True
    >>> import random
    >>> collection = random.sample(range(-50, 50), 100)
    >>> insertion_sort(collection) == sorted(collection)
    True
    >>> import string
    >>> collection = random.choices(string.ascii_letters + string.digits, k=100)
    >>> insertion_sort(collection) == sorted(collection)
    True
    """

    for insert_index in range(1, len(collection)):
        insert_value = collection[insert_index]
        while insert_index > 0 and insert_value < collection[insert_index - 1]:
            collection[insert_index] = collection[insert_index - 1]
            insert_index -= 1
        collection[insert_index] = insert_value
    return collection


if __name__ == "__main__":
    from doctest import testmod

    testmod()

    user_input = input("Enter numbers separated by a comma:\n").strip()
    unsorted = [int(item) for item in user_input.split(",")]
    print(f"{insertion_sort(unsorted) = }")
4 / 6

DIRECTORY.md: The Auto-Generated Map

DIRECTORY.md:1

DIRECTORY.md is the catalog of every algorithm by category. It is auto-regenerated by CI and must never be hand-edited.

A new contributor needs to know two things from this file. First, the catalog is organized by category, with one H2 per directory and one bullet per algorithm; that maps directly to the file system, where every ## Audio Filters entry is a file under audio_filters/. Second, the file is generated. scripts/build_directory_md.py walks the algorithm directories and rewrites this index after every successful CI run, which is why CONTRIBUTING.md ends with the explicit instruction "Do not update the README.md or DIRECTORY.md file which will be periodically autogenerated by our GitHub Actions processes." If you edit this file by hand, the next CI run silently overwrites your change.

Key takeaway

Add your file to the right category directory and let CI regenerate DIRECTORY.md. Hand-editing this file wastes review time because the next build overwrites it.


## Audio Filters
  * [Butterworth Filter](audio_filters/butterworth_filter.py)
  * [Iir Filter](audio_filters/iir_filter.py)
  * [Show Response](audio_filters/show_response.py)

## Backtracking
  * [All Combinations](backtracking/all_combinations.py)
  * [All Permutations](backtracking/all_permutations.py)
  * [All Subsequences](backtracking/all_subsequences.py)
  * [Coloring](backtracking/coloring.py)
  * [Combination Sum](backtracking/combination_sum.py)
  * [Crossword Puzzle Solver](backtracking/crossword_puzzle_solver.py)
  * [Generate Parentheses](backtracking/generate_parentheses.py)
  * [Generate Parentheses Iterative](backtracking/generate_parentheses_iterative.py)
  * [Hamiltonian Cycle](backtracking/hamiltonian_cycle.py)
  * [Knight Tour](backtracking/knight_tour.py)
  * [Match Word Pattern](backtracking/match_word_pattern.py)
  * [Minimax](backtracking/minimax.py)
  * [N Queens](backtracking/n_queens.py)
  * [N Queens Math](backtracking/n_queens_math.py)
  * [Power Sum](backtracking/power_sum.py)
  * [Rat In Maze](backtracking/rat_in_maze.py)
  * [Sudoku](backtracking/sudoku.py)
  * [Sum Of Subsets](backtracking/sum_of_subsets.py)
  * [Word Break](backtracking/word_break.py)
  * [Word Ladder](backtracking/word_ladder.py)
  * [Word Search](backtracking/word_search.py)
5 / 6

pyproject.toml: pytest --doctest-modules Is the Test Suite

pyproject.toml:171

The pytest config runs every doctest in every module as a real test. That is why the doctest convention is non-negotiable.

--doctest-modules on line 177 is the line that makes the doctest convention enforceable. With it set, pytest walks every .py file in the repository, parses every docstring, finds every >>> example, and runs it as a test. The expected output below the prompt is asserted against the actual output. A typo in a docstring example, or an algorithm change that drifts from the documented behavior, fails the build. The same file's [tool.ruff] block (above on line 50) configures the lint gate with about forty rule groups; together they are the two checks every PR must pass. Run them locally with ruff check and pytest before pushing.

Key takeaway

Run ruff check and pytest locally before pushing. The --doctest-modules flag makes every docstring example a real test that fails the build on drift.

[tool.pytest]
ini_options.markers = [
  "mat_ops: mark a test as utilizing matrix operations.",
]
ini_options.addopts = [
  "--durations=10",
  "--doctest-modules",
  "--showlocals",
]
6 / 6

CONTRIBUTING.md: Submission Rules and Where to Go Next

CONTRIBUTING.md:175

The closing checklist enforces snake_case filenames, the no-new-directories rule, and the ban on hand-editing README.md or DIRECTORY.md.

The submission rules collapse into one principle: fit into the existing structure, do not invent your own. Filenames are snake_case (the script that regenerates DIRECTORY.md parses them by that pattern). New top-level directories are discouraged because the catalog already covers 43 categories. Project Euler solutions follow a separate guide at project_euler/README.md. mypy runs on every PR alongside ruff and pytest. When you have a draft, real-time review happens on Discord and Gitter, both linked above. Read the Navigation tour next to see how the 43 categories are laid out, then pick the marquee algorithm tour that matches what you want to add.

Key takeaway

Place your file in an existing category directory, name it snake_case.py, and let CI regenerate the catalogs. Read the Navigation tour next to find your category.


#### Other Requirements for Submissions
- If you are submitting code in the `project_euler/` directory, please also read [the dedicated Guideline](https://github.com/TheAlgorithms/Python/blob/master/project_euler/README.md) before contributing to our Project Euler library.
- The file extension for code files should be `.py`. Jupyter Notebooks should be submitted to [TheAlgorithms/Jupyter](https://github.com/TheAlgorithms/Jupyter).
- Strictly use snake_case (underscore_separated) in your file_name, as it will be easy to parse in future using scripts.
- Please avoid creating new directories if at all possible. Try to fit your work into the existing directory structure.
- If possible, follow the standard *within* the folder you are submitting to.
- If you have modified/added code work, make sure the code compiles before submitting.
- If you have modified/added documentation work, ensure your language is concise and contains no grammar errors.
- Do not update the README.md or DIRECTORY.md file which will be periodically autogenerated by our GitHub Actions processes.
- Add a corresponding explanation to [Algorithms-Explanation](https://github.com/TheAlgorithms/Algorithms-Explanation) (Optional but recommended).
- All submissions will be tested with [__mypy__](http://www.mypy-lang.org) so we encourage you to add [__Python type hints__](https://docs.python.org/3/library/typing.html) where it makes sense to do so.

- Most importantly,
  - __Be consistent in the use of these guidelines when submitting.__
  - __Join__ us on [Discord](https://discord.com/invite/c7MnfGFGa6) and [Gitter](https://gitter.im/TheAlgorithms/community) __now!__
  - Happy coding!

Writer [@poyea](https://github.com/poyea), Jun 2019.
Your codebase next

Create code tours for your project

Intraview lets AI create interactive walkthroughs of any codebase. Install the free VS Code extension and generate your first tour in minutes.

Install Intraview Free