Reference library

Python Code Samples

Copy-ready Python snippets by topic and difficulty — short, focused, and runnable in the browser editor.

4 matches
Comprehensions & generators medium

How to Generate Primes with a Generator in Python

Generate prime numbers up to a limit using the Sieve of Eratosthenes wrapped in a generator expression for lazy evaluation.

generators sieve primes
Python
def prime_generator(limit):
    sieve = [True] * (limit + 1)
    sieve[0] = sieve[1] = False

    for i in range(2, int(limit ** 0.5) + 1):
        if sieve[i]:
            for j in range(i * i, limit + 1, i):
                sieve[j] = False

    return (num for num, is_prime in enumerate(sieve) if is_prime)


if __n…
15 0 Open
Comprehensions & generators easy

How to Lazily Transform Items in Python with a Generator

Map a transform function over an iterable lazily with a generator so items are processed on demand, not up front.

generators lazy evaluation mapping
Python
def lazy_map(items, transform):
    for item in items:
        yield transform(item)

def double(x):
    return x * 2

def upper(s):
    return s.upper()

if __name__ == "__main__":
    numbers = [1, 2, 3, 4, 5]
    doubled = lazy_map(numbers, double)
    print("Doubled numbers:", end=" ")
    for value in doubled:
  …
14 0 Open
Comprehensions & generators medium

How to filter a generator with a predicate function in Python

This code defines a generator function that yields only items from an iterable that satisfy a given predicate, then tests it with even and positive number filters.

generators filtering lazy evaluation
Python
def filter_gen(predicate, iterable):
    for item in iterable:
        if predicate(item):
            yield item

def is_even(num):
    return num % 2 == 0

def is_positive(num):
    return num > 0

if __name__ == "__main__":
    numbers = range(-5, 10)
    
    even_numbers = list(filter_gen(is_even, numbers))
    p…
10 0 Open
Big data & Spark medium

Lazy Evaluation Transform Lineage Mock in Python

Build a mock lineage tracker for data transforms using lazy evaluation and function wrappers in Python.

lazy-evaluation lineage decorator
Python
import functools


def lazy_transform(pipeline):
    """Build a mock lineage tracker using lazy evaluation."""
    lineage = []

    def wrap(func):
        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            result = func(*args, **kwargs)
            lineage.append({"transform": func.__name__, "a…
16 0 Open

Browse by section

Each section groups closely related Python snippets.

Guide: free Python code samples library

Copy-ready Python snippets for learners and developers

PythonSkillset code samples are short, focused examples organised by topic and difficulty. Every snippet is server-rendered HTML — readable by search engines and easy to copy. Open any sample, read the notes, copy the code, then press Try in editor to run it in the browser with Pyodide.

How to use this library

  1. Pick a topic section — strings, lists, files, functions, and more
  2. Open a sample, read How it works, and copy the code block
  3. Run it in the IDE, tweak values, then take a related quiz or tutorial lesson

Samples vs tutorials and challenges

Samples are quick reference — one concept per page. For step-by-step teaching, use our Python tutorials. To test yourself, try quizzes or coding challenges. Clean up style with the Python formatter.