Unit: Unit 6: Functions • Interactive Practice
Interactive Python programming tutorial and practice in your 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"))