Reference library

Functions & basics

Reusable building blocks — parameters, returns, scope, and clear function design.

2 matches
Functions & basics easy

How to Pass a Function as a Callback to map and filter in Python

Shows how to apply custom functions to every element of a list using map and filter callbacks in Python.

map filter callbacks
Python
def double(x):
    return x * 2

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

if __name__ == "__main__":
    numbers = [1, 2, 3, 4, 5]
    doubled = list(map(double, numbers))
    evens = list(filter(is_even, numbers))
    print("Original:", numbers)
    print("Doubled:", doubled)
    print("Evens:", evens)
16 0 Open
Functions & basics easy

Python Filter Function with Default Parameters for Beginners

Create a reusable filter function with default parameters to keep or exclude numbers above or below a threshold.

functions default-parameters filter
Python
def filter_numbers(numbers, threshold=0, reverse=False):
    """Return numbers that pass the threshold filter.

    Args:
        numbers: list of numbers to filter
        threshold: minimum value to keep (default 0)
        reverse: if True, keep numbers below threshold (default False)
    """
    if reverse:
      …
13 0 Open

Browse by section

Each section groups closely related Python snippets.

Functions & basics — Python code examples

What you will find here

This page collects functions & basics snippets — short, copy-ready Python you can paste into our free online IDE and run without installing anything. Each sample includes a plain-English explanation and the full source code.

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.