Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Question: What is nested function in Python?
Answer:

In Python, we can create a function inside another function. This is known as a nested function. For example,

def greet(name):
    # inner function
    def display_name():
        print("Hi", name)
    
    # call inner function
    display_name()

# call outer function
greet("John")  

Output:

Hi John

In the above example, we have defined the display_name() function inside the greet() function.

Here, display_name() is a nested function. The nested function works similar to the normal function. It executes when display_name() is called inside the function greet().

Is it helpful? Yes No

Most helpful rated by users:

©2025 WithoutBook