Reference library

Functions & basics

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

6 matches
Functions & basics easy

How to Document Python Functions with Google Style Docstrings

Document a Python function with a Google style docstring to describe arguments and return values clearly.

docstrings documentation functions
Python
def calculate_rectangle_area(length: float, width: float) -> float:
    """Calculate the area of a rectangle.

    Args:
        length (float): The length of the rectangle in meters.
        width (float): The width of the rectangle in meters.

    Returns:
        float: The area of the rectangle in square meters.
 …
13 0 Open
Functions & basics easy

How to Use Default Parameters with Python's Split Function

Create a reusable Python wrapper around str.split with sensible default parameters for delimiter and maxsplit, showing beginners how default arguments work.

strings default-parameters functions
Python
def split_with_defaults(text, delimiter=" ", maxsplit=-1):
    """
    Split a string into parts using a delimiter.
    Default behavior: split on spaces, unlimited splits.
    """
    parts = text.split(delimiter, maxsplit)
    return parts


if __name__ == "__main__":
    # Example usage with defaults and custom par…
14 0 Open
Functions & basics easy

How to Use a Dispatch Table in Python (Map Strings to Functions)

Maps string command names to callable functions in a dictionary, then dispatches calls safely with error handling.

dispatch-table dictionary functions
Python
def add(a, b):
    return a + b


def subtract(a, b):
    return a - b


def multiply(a, b):
    return a * b


def divide(a, b):
    if b == 0:
        raise ValueError("Division by zero")
    return a / b


dispatch = {
    "add": add,
    "subtract": subtract,
    "multiply": multiply,
    "divide": divide,
}


def…
13 0 Open
Functions & basics easy

How to Use a Lambda Sort Key in Python

Sort a list of strings by length, then alphabetically, using a lambda function as the sorting key in Python.

lambda sorting sorted
Python
def sort_words(words):
    """Sort words by length, then alphabetically using a lambda key."""
    return sorted(words, key=lambda word: (len(word), word))

if __name__ == "__main__":
    sample_words = ["apple", "kiwi", "banana", "fig", "cherry"]
    result = sort_words(sample_words)
    
    print("Original:", sampl…
15 0 Open
Functions & basics easy

How to Use a Lambda Sorting Key in Python

Sort a list of strings by their last letter using a lambda function as the sorting key.

sorting lambda key-function
Python
def get_last_letter(word):
    return word[-1]

words = ["banana", "apple", "cherry", "date", "elderberry"]

if __name__ == "__main__":
    sorted_words = sorted(words, key=get_last_letter)
    print(sorted_words)
12 0 Open
Functions & basics easy

How to Use functools.reduce in Python

Apply functools.reduce with operator functions and lambda expressions to aggregate lists into sums, products, maximums, and concatenated strings.

reduce functools lambda
Python
from functools import reduce
import operator

# Sum all numbers in a list using reduce
numbers = [1, 2, 3, 4, 5]
sum_result = reduce(operator.add, numbers)

# Find the maximum value using reduce
max_result = reduce(lambda a, b: a if a > b else b, numbers)

# Multiply all numbers using reduce
product_result = reduce(la…
12 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.