First-Class Citizens in Python

First-class citizens are entities that support all operations available to other entities in the programming language. In Python, first-class citizens can be passed as arguments, returned from functions, assigned to variables, and stored in data structures.

Python treats many entities as first-class citizens, including data types and functions. This flexibility makes Python a powerful and expressive programming language.

Data Types as First-Class Citizens

The following basic data types are first-class citizens in Python ?

  • Integers
  • Floating-point numbers
  • Complex numbers
  • Strings

Example

Here's how these data types demonstrate first-class behavior ?

# Creating instances of first-class data types
int_value = 42
float_value = 3.14
complex_value = 4+7j
string_value = "TutorialsPoint"

print("Integer type:", type(int_value))
print("Float type:", type(float_value))
print("Complex type:", type(complex_value))
print("String type:", type(string_value))

# Demonstrating first-class behavior - storing in a list
data_types = [int_value, float_value, complex_value, string_value]
print("\nAll values stored in a list:", data_types)
Integer type: <class 'int'>
Float type: <class 'float'>
Complex type: <class 'complex'>
String type: <class 'str'>

All values stored in a list: [42, 3.14, (4+7j), 'TutorialsPoint']

Functions as First-Class Citizens

Python supports first-class functions, meaning functions can be treated as objects. They can be assigned to variables, passed as arguments, and returned from other functions.

Assigning Functions to Variables

# Functions being treated as objects
def check_uppercase(text):
    return text.isupper()

print("Direct function call:", check_uppercase("TUTORIALSPOINT"))

# Assigning function to a variable
function_reference = check_uppercase
print("Using function reference:", function_reference("TutorialsPoint"))
Direct function call: True
Using function reference: False

Passing Functions as Arguments

# Functions being passed as arguments to other functions
def convert_upper(text):
    return text.upper()

def convert_lower(text):
    return text.lower()

def process_text(func, message):
    # Using the passed function
    result = func(message)
    print(result)

# Passing functions as arguments
process_text(convert_upper, "Love Coding, Learn everything on TutorialsPoint")
process_text(convert_lower, "Love Coding, Learn everything on TutorialsPoint")
LOVE CODING, LEARN EVERYTHING ON TUTORIALSPOINT
love coding, learn everything on tutorialspoint

Returning Functions from Functions

# Functions returning other functions
def create_multiplier(factor):
    def multiplier(number):
        return number * factor
    return multiplier

# Creating specialized functions
double = create_multiplier(2)
triple = create_multiplier(3)

print("Double of 5:", double(5))
print("Triple of 4:", triple(4))
Double of 5: 10
Triple of 4: 12

Key Properties of First-Class Citizens

First-class citizens in Python support these operations ?

  • Assignment ? Can be assigned to variables
  • Argument passing ? Can be passed as function arguments
  • Return values ? Can be returned from functions
  • Storage ? Can be stored in data structures like lists or dictionaries
  • Runtime creation ? Can be created at runtime

Conclusion

First-class citizens in Python include data types and functions that can be manipulated like any other object. This feature enables powerful programming patterns like higher-order functions and functional programming techniques in Python.

Updated on: 2026-03-25T06:14:27+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements