{"id":3371,"date":"2023-10-21T15:42:43","date_gmt":"2023-10-21T10:12:43","guid":{"rendered":"https:\/\/python-programs.com\/?p=3371"},"modified":"2023-11-10T11:52:22","modified_gmt":"2023-11-10T06:22:22","slug":"python-how-to-use-if-else-elif-in-lambda-functions","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-how-to-use-if-else-elif-in-lambda-functions\/","title":{"rendered":"Python : How to use if, else and elif in Lambda Functions"},"content":{"rendered":"

Lambda function is the function that is anonymous. This anonymous functions are defined using a lambda keyword.<\/p>\n

In this article we are going to\u00a0 discuss about how to use if , else and elif in a lambda functions.<\/p>\n

Syntax of Lambda function:<\/h3>\n
lambda argument<\/span>s:<\/span> expression<\/pre>\n

So this function can have any number of parameter but have only one expression.That expression should return.<\/p>\n

Here is an example how we are using Lambda function:<\/p>\n

# lambda.py\r\n\r\ncube = lambda x: x * x * x\r\n\r\nprint(cube(11))<\/pre>\n

Output:<\/strong><\/p>\n

lambda.py\r\n\r\n1331<\/span><\/pre>\n

How to use if-else in Lambda function:<\/h3>\n

Here we are going to use if-else in Lambda function.<\/p>\n

lambda <arguments> : <Return<\/span> Value if<\/span> condition is<\/span> True<\/span>> if<\/span> <condition> else<\/span> <Return<\/span> Value if<\/span> condition is<\/span> False<\/span>>\r\n<\/pre>\n

Let’s take an example, Create\u00a0 lambda function to check if the given value is between 11 to 22.<\/p>\n

# lamda.py\r\n\r\nverify = lambda x: True if (x > 9 and x < 18) else False\r\nprint(verify(5))\r\nprint(verify(17))\r\nprint(verify(21))<\/pre>\n

Output:<\/strong><\/p>\n

RESTART: C:\/Users\/HP\/Desktop\/lambda.py\r\nFalse\r\nTrue\r\nFalse\r\n<\/pre>\n

\u00a0Conditional lambda function without if-else in python:<\/h3>\n

Without using if and else keyword we can still get our result using conditional lambda function.For example,<\/p>\n

# lambda.py\r\n\r\nverify = lambda x : x > 9 and x < 18\r\nprint(verify(5))\r\nprint(verify(17))\r\nprint(verify(21))<\/pre>\n

Output:<\/strong><\/p>\n

RESTART: C:\/Users\/HP\/Desktop\/lambda.py\r\n\r\nFalse\r\nTrue\r\nFalse\r\n<\/pre>\n

So we have seen that we can get our result without using if-else statement.<\/p>\n

Using filter() function with the conditional lambda function:<\/h3>\n

This function get a callback() function and a list of elements. It repeats over each and every elements in list and calls the callback() function on every element. If callback() returns True then it will add in new list .In the end, returns the list of items.<\/p>\n

# lambda.py\r\n\r\nlistA = [12, 28, 23, 17, 9, 29, 50]\r\n\r\nlistOutput = list(filter(lambda x: x > 9 and x < 20, listA))\r\nprint(listOutput)<\/pre>\n

Output:<\/strong><\/p>\n

RESTART: C:\/Users\/HP\/Desktop\/lambda.py\r\n[12, 17]\r\n<\/pre>\n

Here we have used lambda function to filter elements and in the end returns list of elements that lies between 9 to 20.<\/p>\n

Using if, elif & else in a lambda function:<\/h3>\n

We can not always depend on if else in a lambda function,sometime there are some possibilities when we need to check on multiple conditions.We can’t directly use else if in a lambda function. But we can fo the same by using if else & brackets .<\/p>\n

Syntax:<\/p>\n

lambda <args> : <return<\/span> Value<\/span>> if<\/span> <condition > ( <return<\/span> value<\/span> > if<\/span> <condition> else<\/span> <return<\/span> value<\/span>>)\r\n<\/pre>\n

Lets take an example:<\/p>\n

lambda.py\r\n\r\nconverter = lambda x : x*2 if x < 11 else (x*3 if x < 22 else x)\r\nprint(converter(5))\r\nprint(converter(24))<\/pre>\n

Output:<\/strong><\/p>\n

lambda.py<\/span>\r\n\r\n10<\/span>\r\n24<\/span>\r\n<\/pre>\n

So, in the above example, 5 * 2 = 10, which is less than 11. So, it gives10 because given condition is true. In the case of 24, condition is false; that is why it returns 24 as .<\/p>\n

Conclusion:<\/h3>\n

So in this article we have seen how to use if , else and elif in a lambda functions.We have also seen how to use conditional statement and use of filter\u00a0 in lambda function.<\/p>\n","protected":false},"excerpt":{"rendered":"

Lambda function is the function that is anonymous. This anonymous functions are defined using a lambda keyword. In this article we are going to\u00a0 discuss about how to use if , else and elif in a lambda functions. Syntax of Lambda function: lambda arguments: expression So this function can have any number of parameter but …<\/p>\n

Python : How to use if, else and elif in Lambda Functions<\/span> Read More »<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[5],"tags":[],"yoast_head":"\nPython : How to use if, else and elif in Lambda Functions - Python Programs<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/python-programs.com\/python-how-to-use-if-else-elif-in-lambda-functions\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python : How to use if, else and elif in Lambda Functions - Python Programs\" \/>\n<meta property=\"og:description\" content=\"Lambda function is the function that is anonymous. This anonymous functions are defined using a lambda keyword. In this article we are going to\u00a0 discuss about how to use if , else and elif in a lambda functions. Syntax of Lambda function: lambda arguments: expression So this function can have any number of parameter but … Python : How to use if, else and elif in Lambda Functions Read More »\" \/>\n<meta property=\"og:url\" content=\"https:\/\/python-programs.com\/python-how-to-use-if-else-elif-in-lambda-functions\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Programs\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/btechgeeks\" \/>\n<meta property=\"article:published_time\" content=\"2023-10-21T10:12:43+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-10T06:22:22+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:site\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Shikha Mishra\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/python-programs.com\/#organization\",\"name\":\"BTech Geeks\",\"url\":\"https:\/\/python-programs.com\/\",\"sameAs\":[\"https:\/\/www.instagram.com\/btechgeeks\/\",\"https:\/\/www.linkedin.com\/in\/btechgeeks\",\"https:\/\/in.pinterest.com\/btechgeek\/\",\"https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg\",\"https:\/\/www.facebook.com\/btechgeeks\",\"https:\/\/twitter.com\/btech_geeks\"],\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"contentUrl\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"width\":350,\"height\":70,\"caption\":\"BTech Geeks\"},\"image\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/python-programs.com\/#website\",\"url\":\"https:\/\/python-programs.com\/\",\"name\":\"Python Programs\",\"description\":\"Python Programs with Examples, How To Guides on Python\",\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/python-programs.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/python-programs.com\/python-how-to-use-if-else-elif-in-lambda-functions\/#webpage\",\"url\":\"https:\/\/python-programs.com\/python-how-to-use-if-else-elif-in-lambda-functions\/\",\"name\":\"Python : How to use if, else and elif in Lambda Functions - Python Programs\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/#website\"},\"datePublished\":\"2023-10-21T10:12:43+00:00\",\"dateModified\":\"2023-11-10T06:22:22+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/python-programs.com\/python-how-to-use-if-else-elif-in-lambda-functions\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/python-programs.com\/python-how-to-use-if-else-elif-in-lambda-functions\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/python-programs.com\/python-how-to-use-if-else-elif-in-lambda-functions\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/python-programs.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python : How to use if, else and elif in Lambda Functions\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/python-programs.com\/python-how-to-use-if-else-elif-in-lambda-functions\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/python-how-to-use-if-else-elif-in-lambda-functions\/#webpage\"},\"author\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/7a690ac49394cc96d2e839bf9a746594\"},\"headline\":\"Python : How to use if, else and elif in Lambda Functions\",\"datePublished\":\"2023-10-21T10:12:43+00:00\",\"dateModified\":\"2023-11-10T06:22:22+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/python-programs.com\/python-how-to-use-if-else-elif-in-lambda-functions\/#webpage\"},\"wordCount\":359,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/python-programs.com\/python-how-to-use-if-else-elif-in-lambda-functions\/#respond\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/7a690ac49394cc96d2e839bf9a746594\",\"name\":\"Shikha Mishra\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/10a27cfafdf21564c686b80411336ece?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/10a27cfafdf21564c686b80411336ece?s=96&d=mm&r=g\",\"caption\":\"Shikha Mishra\"},\"url\":\"https:\/\/python-programs.com\/author\/shikha\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python : How to use if, else and elif in Lambda Functions - Python Programs","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/python-programs.com\/python-how-to-use-if-else-elif-in-lambda-functions\/","og_locale":"en_US","og_type":"article","og_title":"Python : How to use if, else and elif in Lambda Functions - Python Programs","og_description":"Lambda function is the function that is anonymous. This anonymous functions are defined using a lambda keyword. In this article we are going to\u00a0 discuss about how to use if , else and elif in a lambda functions. Syntax of Lambda function: lambda arguments: expression So this function can have any number of parameter but … Python : How to use if, else and elif in Lambda Functions Read More »","og_url":"https:\/\/python-programs.com\/python-how-to-use-if-else-elif-in-lambda-functions\/","og_site_name":"Python Programs","article_publisher":"https:\/\/www.facebook.com\/btechgeeks","article_published_time":"2023-10-21T10:12:43+00:00","article_modified_time":"2023-11-10T06:22:22+00:00","twitter_card":"summary_large_image","twitter_creator":"@btech_geeks","twitter_site":"@btech_geeks","twitter_misc":{"Written by":"Shikha Mishra","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/python-programs.com\/#organization","name":"BTech Geeks","url":"https:\/\/python-programs.com\/","sameAs":["https:\/\/www.instagram.com\/btechgeeks\/","https:\/\/www.linkedin.com\/in\/btechgeeks","https:\/\/in.pinterest.com\/btechgeek\/","https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg","https:\/\/www.facebook.com\/btechgeeks","https:\/\/twitter.com\/btech_geeks"],"logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/","url":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","contentUrl":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","width":350,"height":70,"caption":"BTech Geeks"},"image":{"@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/"}},{"@type":"WebSite","@id":"https:\/\/python-programs.com\/#website","url":"https:\/\/python-programs.com\/","name":"Python Programs","description":"Python Programs with Examples, How To Guides on Python","publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/python-programs.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/python-programs.com\/python-how-to-use-if-else-elif-in-lambda-functions\/#webpage","url":"https:\/\/python-programs.com\/python-how-to-use-if-else-elif-in-lambda-functions\/","name":"Python : How to use if, else and elif in Lambda Functions - Python Programs","isPartOf":{"@id":"https:\/\/python-programs.com\/#website"},"datePublished":"2023-10-21T10:12:43+00:00","dateModified":"2023-11-10T06:22:22+00:00","breadcrumb":{"@id":"https:\/\/python-programs.com\/python-how-to-use-if-else-elif-in-lambda-functions\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/python-programs.com\/python-how-to-use-if-else-elif-in-lambda-functions\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/python-programs.com\/python-how-to-use-if-else-elif-in-lambda-functions\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/python-programs.com\/"},{"@type":"ListItem","position":2,"name":"Python : How to use if, else and elif in Lambda Functions"}]},{"@type":"Article","@id":"https:\/\/python-programs.com\/python-how-to-use-if-else-elif-in-lambda-functions\/#article","isPartOf":{"@id":"https:\/\/python-programs.com\/python-how-to-use-if-else-elif-in-lambda-functions\/#webpage"},"author":{"@id":"https:\/\/python-programs.com\/#\/schema\/person\/7a690ac49394cc96d2e839bf9a746594"},"headline":"Python : How to use if, else and elif in Lambda Functions","datePublished":"2023-10-21T10:12:43+00:00","dateModified":"2023-11-10T06:22:22+00:00","mainEntityOfPage":{"@id":"https:\/\/python-programs.com\/python-how-to-use-if-else-elif-in-lambda-functions\/#webpage"},"wordCount":359,"commentCount":0,"publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/python-programs.com\/python-how-to-use-if-else-elif-in-lambda-functions\/#respond"]}]},{"@type":"Person","@id":"https:\/\/python-programs.com\/#\/schema\/person\/7a690ac49394cc96d2e839bf9a746594","name":"Shikha Mishra","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/10a27cfafdf21564c686b80411336ece?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/10a27cfafdf21564c686b80411336ece?s=96&d=mm&r=g","caption":"Shikha Mishra"},"url":"https:\/\/python-programs.com\/author\/shikha\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/3371"}],"collection":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/comments?post=3371"}],"version-history":[{"count":2,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/3371\/revisions"}],"predecessor-version":[{"id":4396,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/3371\/revisions\/4396"}],"wp:attachment":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/media?parent=3371"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/categories?post=3371"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/tags?post=3371"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}