Basic Python 26: Anonymous Function | Lambda Function in Python

Published: 03 October 2022
on channel: Testing Tutorialspoint
19
5

Python Anonymous/Lambda Function
------------------------------------------------------------
What are lambda functions in Python?

an anonymous function is a function that is defined without a name.

While normal functions are defined using the def keyword in Python, anonymous functions are defined using the lambda keyword.

Anonymous functions are also called lambda functions.

syntax:
lambda arguments: expression

Lambda functions can have any number of arguments but only one expression. The expression is evaluated and returned. Lambda functions can be used wherever function objects are required.
ex:
double = lambda x: x * 2

print(double(5))

Use of Lambda Function in python:

We use lambda functions when we require a nameless function for a short period of time.
we generally use it as an argument to a higher-order function (a function that takes in other functions as arguments).
Lambda functions are used along with built-in functions like filter(), map() etc.

ex:using filter

The filter() function in Python takes in a function and a list as arguments.

The function is called with all the items in the list and a new list is returned which contains items for which the function evaluates to True.

my_list = [1, 5, 4, 6, 8, 11, 3, 12]

new_list = list(filter(lambda x: (x%2 == 0) , my_list))

print(new_list)

Example use with map():
-----------------------
The map() function in Python takes in a function and a list.

The function is called with all the items in the list and a new list is returned which contains items returned by that function for each item.

my_list = [1, 5, 4, 6, 8, 11, 3, 12]

new_list = list(map(lambda x: x * 2 , my_list))

print(new_list)
__________________ API Automation _________________
➡️ Rest Assured Using Java →    • Rest Assured  
➡️ Karate Framework using Maven →    • Karate Framework Using Maven Project  
_____________ Programing Language ____________________
➡️ Basic Python →    • Basic Python  
➡️ Core Java →    • CoreJava  
___________ Performances Testing ___________________
➡️ JMeter Beginner →    • JMeter Beginner  
➡️ Locust Beginner →    • Locust  
___________ Git and GitHub _____________________________
➡️ Git and GitHub Beginner →    • Git and GitHub Beginner  

_____________Manual Testing ____________________
➡️ Manual Testing →    • Manual Testing  

______________Automation Testing __________________
➡️ Selenium Cucumber Framework using Java →    • Selenium Cucumber BDD Framework with Java ...  
➡️ Robot Framework with Python →    • Python With Robot Framework  
➡️ Beginner Karate Framework using Intellij →    • Karate Framework Beginner  
➡️ Karate Framework with Gradle using eclipse →    • Karate Framework using Gradle Project  
➡️ Basic Selenium WebDriver using Java →    • Selenium WebDriver  
➡️ TestNG Framework →    • TestNG Framework  
➡️ Robot Framework with RIDE →    • RIDE With Robot Framework  
________________ Beginner Jenkins ____________________
➡️ Beginner Jenkins →    • Beginner Jenkins  


On this page of the site you can watch the video online Basic Python 26: Anonymous Function | Lambda Function in Python with a duration of hours minute second in good quality, which was uploaded by the user Testing Tutorialspoint 03 October 2022, share the link with friends and acquaintances, this video has already been watched 19 times on youtube and it was liked by 5 viewers. Enjoy your viewing!