Diving Into Python: Chapter 14

This is the 14th part of this article series about Python This series was only for taking you through the very basics concepts of Python. Afterward in the second version of this article series, I'll take you through some advanced concepts.
 
For getting the theme of Python, kindly go through my previous articles:
Anonymous / Lambda Function
 
An anonymous function is a function that doesn't require any name or we can say that it can be defined without using a def keyword, unlike normal functions creation in the Python.
 
In Python, an anonymous function is defined using the "Lambda" keyword. That's why it is also called a Lambda function.
 
Example
  1. find_val = lambda x : x*x+2  
  2. print (find_val(2)) 
Output:
 
Output
 
Why Anonymous Functions
 
Now, there might be a question in your mind, what is the real use of a lambda / anonymous function in Python, since we have already general functions available (using the def keyword). Actually, in Python, we use anonymous functions when we require a nameless for a short span of time.
 
Anonymous functions are actually quite useful. Python supports a style of programming called functional programming where you can pass functions to other functions to do some really cool stuff.
 
The preceding argument may sound unfamiliar, let me explain in short. We do use an anonymous function as an argument to a higher-order function.
 
Let's take a close look at some example for a better understanding:
 
Example:
 
In a simple program of sorting out even numbers from a list, using the traditional function, the function name is defined using def the keyword. Have a look:
  1. def myfunc(x):  
  2.        return (x % 2 == 0)  
  3. even_num = list(filter(myfunc, [123456789]))  
  4. print (even_num) 
Explanation
 
It will give you [2, 4, 6, 8] for sure.
 
Anonymous Function
 
Now let's do the same thing using an anonymous method in Python, using the lambda keyword. Have a look:
  1. even_num = list(filter(lambda x: x % 2 == 0, [123456789]))  
  2. print (even_num) 
Explanation
 
This method will also produce the same result.
 
method in python
 
Built-In Functions
 
We generally use Anonymous Functions along with some built-in functions, these are:
 
Built In Functions
 
Let's explore them in detail.
  • Filter() Function
     
    As I discussed earlier, a filter() function takes a function and a list as arguments and proceeds functioning on them.
     
    Example
    1. pos_int = [12345678910]  
    2. even_num = list(filter(lambda x: (x%2 == 0) , pos_int))  
    3. print(even_num) 
    Output
     
    Filter Function
     
    Explanation
     
    Here we are passing a number of arguments in a list and a filter function is doing its job, that is nothing but taking a defined function pos_int and listing arguments.
     
    Without a filter function
     
    To do that without a filter function it will surely give you a type error. Let's see what this type of error is saying (making some changes in the code).
    1. pos_int = [12345678910]  
    2. even_num = list(lambda x: (x%2 == 0) , pos_int)  
    3. print(even_num) 
    Result
     
    As defined:
     
    Result
     
  • Map() Function
     
    The map() function in Python takes in a function and a respective list for functioning. The function is called with all the items in the list and a new list is returned containing items returned by that function for each item.
     
    Example
    1. pos_int = [12345678910]  
    2. even_num = list(map(lambda x: (x%2 == 0) , pos_int))  
    3. print(even_num) 
    Output
     
    Map Function
     
    Explanation
     
    Here we are passing a number of arguments in the list and the map function is doing the rest of the job, something like boolean operations.
     
    Without a map function in our snippet
     
    If we don't use the map function in our snippet then it will surely give you a type error, the same as in the case of the filter function. Let's see what this type of error is saying (making some changes in the code).
    1. pos_int = [12345678910]  
    2. even_num = list(lambda x: (x%2 == 0) , pos_int)  
    3. print(even_num) 
    Result
     
    As defined:
     
    map function in our snippet
PinPoints
  • An anonymous function cannot be directly called to the print, because unlike a normal function, a lambda requires an expression.
  • It can take any number of the arguments but returns only one value in the form of expression.
  • Anonymous functions have their own local namespace.
  • Anonymous functions can't access variables other than those in their functioning parenthesis.
Guidelines from my side
  • Do as much as code you can.
  • Code anything you want, the best way to learn.
  • Don't just study things, try to learn them.
  • Work on your concepts.
  • Work on the fundamentals of any technology or stuff you want to learn.
Python Says
 
Python Says
 
Moto
 
“Keep calm and code Python”.
 
I tried to make this an interesting and interactive article and wish you guys like that, meanwhile, if you have any suggestions then your welcome.
 
This series was only for taking you through the very basics concepts of Python. Afterward in the second version of this article series, I'll take you through some advanced concepts.
 
Until the advanced stuff, work on your basics if you began learning and keep sharing!


Similar Articles