{"id":191,"date":"2019-02-08T03:06:28","date_gmt":"2019-02-08T03:06:28","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/python\/?p=191"},"modified":"2020-07-26T09:41:20","modified_gmt":"2020-07-26T09:41:20","slug":"16-python-anonymous-functions","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/python\/16-python-anonymous-functions\/","title":{"rendered":"Python &#8211; Anonymous Functions"},"content":{"rendered":"<p>We would cover anonymous functions. These are functions that you create without the def keyword. You declare then using the lambda keyword. That is why they are also called lambda expressions.<\/p>\n<p>You can review<a href=\"https:\/\/www.kindsonthegenius.com\/python\/python-functions\/\" target=\"_blank\" rel=\"noopener\"> Functions in Python.<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>The following applies to anonymous functions in Python:<\/p>\n<ul>\n<li>they can take any number of arguments. However, they return only one value in form of an expression.<\/li>\n<li>they cannot contain multiple expressions or commands<\/li>\n<li>anonymous functions can only access variables that have been provided in their parameter list<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><strong>How to create an anonymous function<\/strong><\/p>\n<p>You create a anonymous function using the lambda keyword, then followed by a list of parameters. Then followed by a colon, then the expression.<\/p>\n<p>For example, the code below is a lambda expression that calculates average of three numbers<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\"># lambda expression to calculate average<\/span>\r\naverage <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">lambda<\/span> num1, num2, num3: (num1 <span style=\"color: #333333;\">+<\/span> num2 <span style=\"color: #333333;\">+<\/span> num3)<span style=\"color: #333333;\">\/<\/span><span style=\"color: #0000dd; font-weight: bold;\">3\r\n<\/span>\r\n<\/pre>\n<p>Now you can see that it is just a one-line function. The parameters num1, num2 and num3 are not enclosed in parentheses, as you can see<\/p>\n<p>Let&#8217;s now write this same function using the normal def keyword, so we can see the difference<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\"># equivalent function using def<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">average<\/span>(num1, num2, num3):\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> (num1 <span style=\"color: #333333;\">+<\/span> num2 <span style=\"color: #333333;\">+<\/span> num3) <span style=\"color: #333333;\">\/<\/span> <span style=\"color: #0000dd; font-weight: bold;\">3<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>The lambda expression below would print out the total of three numbers.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">total <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">lambda<\/span> num1, num2, num3: <span style=\"color: #007020;\">print<\/span>(num1 <span style=\"color: #333333;\">+<\/span> num2 <span style=\"color: #333333;\">+<\/span> num3)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>To use the above function you just call it like every other function. For example, below is a code to call the lambda expression.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">total(<span style=\"color: #0000dd; font-weight: bold;\">4<\/span>,<span style=\"color: #0000dd; font-weight: bold;\">6<\/span>,<span style=\"color: #0000dd; font-weight: bold;\">7<\/span>)\r\n<\/pre>\n<p>If you run this code, then it would call the lambda expression and print out the sum of 4, 6 and 7<\/p>\n<p>&nbsp;<\/p>\n<p><strong>When to use lambda expressions<\/strong><\/p>\n<p>Since lambda expressions are just one-line of code, it would not be best write very large and complex functions with them. So you need to decide.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We would cover anonymous functions. These are functions that you create without the def keyword. You declare then using the lambda keyword. That is why &hellip; <\/p>\n","protected":false},"author":395,"featured_media":194,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[5],"tags":[15,16,14],"class_list":["post-191","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorials","tag-lambda-expressions","tag-python-lambda-expressions","tag-python-anonymous-functions"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python - Anonymous Functions - Python Tutorials<\/title>\n<meta name=\"description\" content=\"This lesson explains anonymous functions also known as lambda expressions in Python. You also learn how to write lambda expressions in Python\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.kindsonthegenius.com\/python\/16-python-anonymous-functions\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python - Anonymous Functions - Python Tutorials\" \/>\n<meta property=\"og:description\" content=\"This lesson explains anonymous functions also known as lambda expressions in Python. You also learn how to write lambda expressions in Python\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.kindsonthegenius.com\/python\/16-python-anonymous-functions\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Tutorials\" \/>\n<meta property=\"article:published_time\" content=\"2019-02-08T03:06:28+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-07-26T09:41:20+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.kindsonthegenius.com\/python\/wp-content\/uploads\/sites\/8\/2019\/02\/Lambda-Expression-Anonymous-funcitons-in-Python.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1005\" \/>\n\t<meta property=\"og:image:height\" content=\"574\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"kindsonthegenius\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"kindsonthegenius\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/16-python-anonymous-functions\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/16-python-anonymous-functions\\\/\"},\"author\":{\"name\":\"kindsonthegenius\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\"},\"headline\":\"Python &#8211; Anonymous Functions\",\"datePublished\":\"2019-02-08T03:06:28+00:00\",\"dateModified\":\"2020-07-26T09:41:20+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/16-python-anonymous-functions\\\/\"},\"wordCount\":269,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/16-python-anonymous-functions\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/wp-content\\\/uploads\\\/sites\\\/8\\\/2019\\\/02\\\/Lambda-Expression-Anonymous-funcitons-in-Python.jpg\",\"keywords\":[\"Lambda Expressions\",\"Python - Lambda Expressions\",\"Python Anonymous functions\"],\"articleSection\":[\"Python Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/16-python-anonymous-functions\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/16-python-anonymous-functions\\\/\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/16-python-anonymous-functions\\\/\",\"name\":\"Python - Anonymous Functions - Python Tutorials\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/16-python-anonymous-functions\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/16-python-anonymous-functions\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/wp-content\\\/uploads\\\/sites\\\/8\\\/2019\\\/02\\\/Lambda-Expression-Anonymous-funcitons-in-Python.jpg\",\"datePublished\":\"2019-02-08T03:06:28+00:00\",\"dateModified\":\"2020-07-26T09:41:20+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\"},\"description\":\"This lesson explains anonymous functions also known as lambda expressions in Python. You also learn how to write lambda expressions in Python\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/16-python-anonymous-functions\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/16-python-anonymous-functions\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/16-python-anonymous-functions\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/wp-content\\\/uploads\\\/sites\\\/8\\\/2019\\\/02\\\/Lambda-Expression-Anonymous-funcitons-in-Python.jpg\",\"contentUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/wp-content\\\/uploads\\\/sites\\\/8\\\/2019\\\/02\\\/Lambda-Expression-Anonymous-funcitons-in-Python.jpg\",\"width\":1005,\"height\":574,\"caption\":\"Lambda Expression in Python\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/16-python-anonymous-functions\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python &#8211; Anonymous Functions\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/#website\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/\",\"name\":\"Python Tutorials\",\"description\":\"Python Tutorial for Programming and Data Science\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\",\"name\":\"kindsonthegenius\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"caption\":\"kindsonthegenius\"},\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/author\\\/kindsonthegenius-2\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python - Anonymous Functions - Python Tutorials","description":"This lesson explains anonymous functions also known as lambda expressions in Python. You also learn how to write lambda expressions in Python","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:\/\/www.kindsonthegenius.com\/python\/16-python-anonymous-functions\/","og_locale":"en_US","og_type":"article","og_title":"Python - Anonymous Functions - Python Tutorials","og_description":"This lesson explains anonymous functions also known as lambda expressions in Python. You also learn how to write lambda expressions in Python","og_url":"https:\/\/www.kindsonthegenius.com\/python\/16-python-anonymous-functions\/","og_site_name":"Python Tutorials","article_published_time":"2019-02-08T03:06:28+00:00","article_modified_time":"2020-07-26T09:41:20+00:00","og_image":[{"width":1005,"height":574,"url":"https:\/\/www.kindsonthegenius.com\/python\/wp-content\/uploads\/sites\/8\/2019\/02\/Lambda-Expression-Anonymous-funcitons-in-Python.jpg","type":"image\/jpeg"}],"author":"kindsonthegenius","twitter_card":"summary_large_image","twitter_misc":{"Written by":"kindsonthegenius","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.kindsonthegenius.com\/python\/16-python-anonymous-functions\/#article","isPartOf":{"@id":"https:\/\/www.kindsonthegenius.com\/python\/16-python-anonymous-functions\/"},"author":{"name":"kindsonthegenius","@id":"https:\/\/www.kindsonthegenius.com\/python\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2"},"headline":"Python &#8211; Anonymous Functions","datePublished":"2019-02-08T03:06:28+00:00","dateModified":"2020-07-26T09:41:20+00:00","mainEntityOfPage":{"@id":"https:\/\/www.kindsonthegenius.com\/python\/16-python-anonymous-functions\/"},"wordCount":269,"commentCount":0,"image":{"@id":"https:\/\/www.kindsonthegenius.com\/python\/16-python-anonymous-functions\/#primaryimage"},"thumbnailUrl":"https:\/\/www.kindsonthegenius.com\/python\/wp-content\/uploads\/sites\/8\/2019\/02\/Lambda-Expression-Anonymous-funcitons-in-Python.jpg","keywords":["Lambda Expressions","Python - Lambda Expressions","Python Anonymous functions"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.kindsonthegenius.com\/python\/16-python-anonymous-functions\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.kindsonthegenius.com\/python\/16-python-anonymous-functions\/","url":"https:\/\/www.kindsonthegenius.com\/python\/16-python-anonymous-functions\/","name":"Python - Anonymous Functions - Python Tutorials","isPartOf":{"@id":"https:\/\/www.kindsonthegenius.com\/python\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.kindsonthegenius.com\/python\/16-python-anonymous-functions\/#primaryimage"},"image":{"@id":"https:\/\/www.kindsonthegenius.com\/python\/16-python-anonymous-functions\/#primaryimage"},"thumbnailUrl":"https:\/\/www.kindsonthegenius.com\/python\/wp-content\/uploads\/sites\/8\/2019\/02\/Lambda-Expression-Anonymous-funcitons-in-Python.jpg","datePublished":"2019-02-08T03:06:28+00:00","dateModified":"2020-07-26T09:41:20+00:00","author":{"@id":"https:\/\/www.kindsonthegenius.com\/python\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2"},"description":"This lesson explains anonymous functions also known as lambda expressions in Python. You also learn how to write lambda expressions in Python","breadcrumb":{"@id":"https:\/\/www.kindsonthegenius.com\/python\/16-python-anonymous-functions\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.kindsonthegenius.com\/python\/16-python-anonymous-functions\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.kindsonthegenius.com\/python\/16-python-anonymous-functions\/#primaryimage","url":"https:\/\/www.kindsonthegenius.com\/python\/wp-content\/uploads\/sites\/8\/2019\/02\/Lambda-Expression-Anonymous-funcitons-in-Python.jpg","contentUrl":"https:\/\/www.kindsonthegenius.com\/python\/wp-content\/uploads\/sites\/8\/2019\/02\/Lambda-Expression-Anonymous-funcitons-in-Python.jpg","width":1005,"height":574,"caption":"Lambda Expression in Python"},{"@type":"BreadcrumbList","@id":"https:\/\/www.kindsonthegenius.com\/python\/16-python-anonymous-functions\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.kindsonthegenius.com\/python\/"},{"@type":"ListItem","position":2,"name":"Python &#8211; Anonymous Functions"}]},{"@type":"WebSite","@id":"https:\/\/www.kindsonthegenius.com\/python\/#website","url":"https:\/\/www.kindsonthegenius.com\/python\/","name":"Python Tutorials","description":"Python Tutorial for Programming and Data Science","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.kindsonthegenius.com\/python\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.kindsonthegenius.com\/python\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2","name":"kindsonthegenius","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","caption":"kindsonthegenius"},"url":"https:\/\/www.kindsonthegenius.com\/python\/author\/kindsonthegenius-2\/"}]}},"_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/posts\/191","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/users\/395"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/comments?post=191"}],"version-history":[{"count":2,"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/posts\/191\/revisions"}],"predecessor-version":[{"id":382,"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/posts\/191\/revisions\/382"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/media\/194"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/media?parent=191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/categories?post=191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/tags?post=191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}