VOOZH about

URL: https://www.geeksforgeeks.org/python/first-class-functions-python/

⇱ First Class functions in Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

First Class functions in Python

Last Updated : 12 Mar, 2026

In Python, functions are treated as first-class objects. This means they can be used just like numbers, strings, or any other variable. You can:

  • Assign functions to variables.
  • Pass them as arguments to other functions.
  • Return them from functions.
  • Store them in data structures such as lists or dictionaries.

This ability allows you to write reusable, modular and powerful code.

Characteristics of First-Class Functions

Functions in Python have the following important characteristics. Let’s see them one by one with examples:

1. Assigning Functions to Variables

We can assign a function to a variable and use the variable to call the function. Example:


Output
Hello, Emma!

Explanation:

  • The function msg is assigned to the variable f.
  • Now f can be used to call msg, showing that functions behave like variables.

2. Passing Functions as Arguments

Functions can be passed as arguments to other functions, enabling higher-order functions.


Output
Hello, Alex!

Explanation:

  • The function fun1 takes another function (fun2) as input.
  • msg is passed to fun1, which then calls it with "Alex".

3. Returning Functions from Other Functions

A function can return another function, allowing for the creation of function factories.


Output
Message: Hello, World!

Explanation:

  • The function fun1 defines another function fun2 and returns it.
  • func stores the returned function fun2, which can be executed later.

4. Storing Functions in Data Structures

Functions can be stored in data structures like lists or dictionaries.


Output
8
2

Explanation:

  • Functions add and subtract are stored in a dictionary.
  • They are accessed using their keys and executed directly.
Comment
Article Tags:
Article Tags: