Python allows you to define nested functions. Let’s see it with an example.
What is a nested function?
Function declared inside another function is called nested function.
nested_function_demo.py
def arithmetic(a, b):
def sum(a, b):
return a + b
def sub(a, b):
return a - b
def mul(a, b):
return a * b
def div(a, b):
return a / b
print('Sum of ', a, ' and ', b, ' is : ', sum(a, b))
print('Subtraction of ', a, ' and ', b, ' is : ', sub(a, b))
print('Multiplication of ', a, ' and ', b, ' is : ', mul(a, b))
print('Division of ', a, ' and ', b, ' is : ', div(a, b))
arithmetic(10, 20)
Output
Sum of 10 and 20 is : 30 Subtraction of 10 and 20 is : -10 Multiplication of 10 and 20 is : 200 Division of 10 and 20 is : 0.5
Previous Next Home
No comments:
Post a Comment