0Pricing
Python Academy · Lesson

First-Class Functions

Pass and return functions.

First-Class Functions is a free Python Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Functions are objects

In Python, functions are first-class objects. They can be assigned to variables, stored in data structures, passed as arguments, and returned from other functions — just like numbers or strings.

def greet():
    return 'hi'
print(type(greet))
print(greet())

Assigning functions to variables

A function name is just a reference. Assign it to another name and call through that name — no parentheses when referring to the function itself.

def shout(text):
    return text.upper()
yell = shout
print(yell('hello'))

Functions in data structures

You can store functions in lists or dicts, then look them up and call them dynamically.

def add(a, b):
    return a + b
def sub(a, b):
    return a - b
ops = {'+': add, '-': sub}
print(ops['+'](10, 4))
print(ops['-'](10, 4))

Passing functions as arguments

A function that takes another function as a parameter is called a higher-order function. The passed function is the argument.

def apply(func, value):
    return func(value)
print(apply(str.upper, 'cat'))
print(apply(len, 'cat'))

map uses first-class functions

Built-ins like map rely on passing functions around. Here a function is applied to every item.

def square(n):
    return n * n
result = list(map(square, [1, 2, 3, 4]))
print(result)

filter takes a predicate

filter keeps items for which the passed function returns True. The function decides what stays.

def is_even(n):
    return n % 2 == 0
print(list(filter(is_even, range(10))))

Returning a function

A function can return another function. The returned function can then be called separately.

def get_greeter():
    def greeter(name):
        return 'Hello ' + name
    return greeter
f = get_greeter()
print(f('Ada'))

Functions as sort keys

sorted accepts a key function that maps each item to a value used for ordering.

words = ['banana', 'fig', 'cherry']
by_length = sorted(words, key=len)
print(by_length)

Lambdas as quick functions

A lambda is a small anonymous function. It is perfect for one-off use as an argument.

nums = [5, 1, 4, 2]
print(sorted(nums, key=lambda x: -x))

Functions have attributes

Since functions are objects, they carry attributes like __name__ and a docstring in __doc__.

def hello():
    'Says hello.'
    return 'hi'
print(hello.__name__)
print(hello.__doc__)

Building a dispatch table

Combining these ideas, you can build a clean dispatch table that picks a function by name — no long if/elif chains.

def start():
    return 'starting'
def stop():
    return 'stopping'
actions = {'start': start, 'stop': stop}
command = 'start'
print(actions[command]())

Quick Check

What does it mean that functions are 'first-class' in Python?

Recap: First-Class Functions

You learned that in Python:

  • Functions are objects you can assign, store, pass, and return.
  • Higher-order functions take or return other functions (map, filter, sorted key).
  • lambda creates quick anonymous functions.
  • This enables clean dispatch tables instead of long conditionals.
ops = {'double': lambda x: x * 2}
print(ops['double'](21))

Frequently asked questions

Is the “First-Class Functions” lesson free?

Yes — the full text of “First-Class Functions” is free to read here on the web, and the Python Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.

What will I learn in “First-Class Functions”?

Pass and return functions. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Python Academy?

No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “First-Class Functions” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Python Academy lesson?

Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. First-Class Functions
  2. Closures and Free Variables
  3. nonlocal and Mutable State
  4. Partial Functions
← Back to Python Academy