Python Inner Function
Python Inner Function
FUNCTION
FUNCTION IN FUNCTION
DEFINING A FUNCTION IN A FUNCTION
Inner Function
DEFINING A FUNCTION IN A FUNCTION
• In Python, an inner function is a function defined inside
another function. The inner function can access variables
and parameters of the outer function and can also be used
to implement closures.
• Here is an example of defining an inner function:
In the above example, we have defined an outer function
called outer_function() and an inner function called inner_function().
The inner_function() is defined within the body of outer_function(). Note
that the inner function is only called within the outer function.
This code defines a function called create_list that allows the user to add,
delete, and display a list of names. Here is a step-by-step breakdown of the code:
1.The function create_list is defined. Inside the function, an empty list called names_list is
defined to store the names.
1.The function add_name is defined inside the create_list function. This function asks the user to
enter a name and appends it to the names_list using the append method. A message is displayed to
confirm that the name has been added to the list.
1.The function delete_name is defined inside the create_list function. This function asks the
user to enter a name and checks if it is in the names_list. If the name is in the list, it is removed using
the remove method. A message is displayed to confirm that the name has been deleted from the list. If
the name is not in the list, a message is displayed to inform the user.
1.The function display_list is defined inside the create_list function. This function simply prints all the names in
the names_list using a for loop.
2.Inside the while loop, the user is presented with a menu of options to choose from. They can choose to add a name to the
list, delete a name from the list, display the recorded list, or quit the program.
3.Depending on the user's choice, the corresponding function is called. If the user chooses to quit, the loop is broken and the
function exits.
4.If the user enters an invalid choice, a message is displayed to inform the user, and the loop continues.
1.Finally, the function create_list is called to run the program.
Overall, this code provides a simple way for the user to interact with a list of names
and add, delete, or display them as desired.
Anonymous Functions: lambda
WHAT IS LAMBDA FUNCTION IN PYTHON?
• A Lambda Function in Python programming is an anonymous
function or a function having no name. It is a small and restricted
function having no more than one line. Just like a normal function,
a Lambda function can have multiple arguments with one
expression.
Every anonymous function you define in Python will have 3
essential parts:
• Now that you know about lambdas let’s try it with an example. So,
open your IDLE and type in the following:
Code Explanation
Here, we define a variable that will hold the result returned by the lambda
function.
1. The lambda keyword used to define an anonymous function.
2. x and y are the parameters that we pass to the lambda function.
3. This is the body of the function, which adds the 2 parameters we passed.
Notice that it is a single expression. You cannot write multiple statements in the
body of a lambda function.
4. We call the function and print the returned value.
In Python, an anonymous function is a function that is defined
without a name. While normal functions are defined using
the def keyword
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s R e s e r v e d | c t u t r a i n i n g . a c . z a
Scope
In programming, a namespace is a container for a set of identifiers (such as
variables, functions, and classes) that are used to organize code and prevent
naming conflicts. A namespace can be thought of as a dictionary that maps names
to objects, where each namespace has its own dictionary.
Scope, on the other hand, refers to the region of the code where a
particular name can be accessed. In Python, there are four levels of
scope:
The try block contains the code that you want to run, while
The except block contains the code that will be executed if an exception
occurs in the try block. If no exception occurs in the try block, the except block
will be skipped.
Exception Types in PYTHON
4/23/2024 Sample Footer Text 46
Main Heading | Subheading
The End
2 0 2 02 0C2T0U CTrT aUi nTri nagi nSi nogl uSt iool nu st i o|n sA l |l RAi gl l hRt si gRhet s eRr ev seedr v |e dc t u
| t rcat u
i nt irnagi n. ai nc g
. z. a c . z a