liveBook · Manning

Lambda

As explained in Build Python Web Apps with Streamlit

A lambda function is an anonymous Python function defined as a single expression, typically for short computations.

A lambda is an anonymous Python function written as a single expression. It accepts arguments, performs a short calculation, and returns the result without needing a function name. section 2.5.3

Lambdas work well when a small operation belongs directly inside a data structure or configuration. They keep short checks close to the rules they implement, while longer or more defensive calculations are clearer as regular functions. section 2.5.3 section 6.4.2

Checking password rules

A Streamlit password checker stores rules in a dictionary: `lambda pw: len(pw) > 8` checks length, while other lambdas use `any` to find uppercase, lowercase, or special characters. Each function returns True or False, and a dictionary comprehension evaluates every rule. section 2.5.3

Configuring dashboard metrics

A metric configuration assigns `func=lambda df: df["Sales"].sum()` to calculate total sales. Metrics needing zero-denominator protection use named functions instead, keeping the calculation explicit and safe. section 6.4.2

Listing 2.1 The code for the password checker app
import streamlit as st

# Add your own conditions if you like
conditions = {
    'More than 8 characters': lambda pw: len(pw) > 8,
    'At least one uppercase letter':
        lambda pw: any(char.isupper() for char in pw),
    'At least one lowercase letter':
        lambda pw: any(char.islower() for char in pw),
    'At least one special character':
        lambda pw: any(char in ",.!?@#$%^&*()-_+=|\\/:;<>~" for char in pw),
}

def get_password_properties(password):
    return {cond: check(password) for cond, check in conditions.items()}

st.title("Password Checker")
password_input = st.text_input("Enter your password", type="password")

if st.button("Check password"):
    if password_input:
        properties = get_password_properties(password_input)

        # Loop through password conditions and show the status for each
        for condition, passes in properties.items():
            if passes:
                st.success(f'✔ Pass: {condition}')
            else:
                st.error(f'✖ Fail: {condition}')
    else:
        st.write("Please enter a password.")

Inspect the complete password-checker listing to see lambdas connected to user input, button handling, rule evaluation, and visible success or error messages. section 2.5.3

How Build Python Web Apps with Streamlit covers it

How deep it goes
Defines single-expression functions and demonstrates evaluation, but stops short of broader lambda mechanics.
Examples
Demonstrates password-rule predicates and sales or gross-margin aggregation in metric configuration.
What it assumes
Uses Python arguments, dictionaries, comprehensions, generators, and pandas dataframe operations in its examples.
What you can do after
Apply lambdas to compact validation rules and dataframe metric calculations, choosing regular functions for longer guarded logic.
How it connects
Connects lambdas with dictionaries, dictionary comprehensions, Streamlit, pandas dataframes, and regular functions.

Also explained in

Compare how 18 books explain lambda →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Python Web Apps with Streamlit. The section links point to the parts of the book it draws on.