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

















