Courses/Functional Python

λ Functional Python

Pure Functions & Immutability

Write code that's easy to test and reason about.

A *pure* function returns the same output for the same input and has no side effects — no mutating globals, no printing, no I/O.

# Impure: mutates the argument
def add_impure(xs, x):
    xs.append(x)
    return xs

# Pure: returns a new list
def add_pure(xs, x):
    return [*xs, x]

Pure functions compose freely and parallelize safely.

main.py
Output
Press Run to execute.
Expected output
[1, 2, 3]
[1, 2, 3, 4]

Sign in to track your progress across lessons.