6.3.1 First-Class Functions: Functions as Objects

Unit: Unit 6: Functions • Interactive Practice

Quick Answer & Summary:

Interactive Python programming tutorial and practice in your browser.

Interactive Challenge (Run Live in Browser):

# Functions are first-class objects in Python
def shout(text):
    return text.upper() + "!"

def whisper(text):
    return text.lower() + "..."

# 1. Assign function to another variable
speaker = shout
print("Via speaker variable:", speaker("hello"))

# 2. Store functions in a dictionary (command dispatch table)
commands = {
    "loud": shout,
    "quiet": whisper
}

print("Dispatch loud:", commands["loud"]("system operational"))
print("Dispatch quiet:", commands["quiet"]("System Operational"))
Open in Live Interactive Python Sandbox →