{"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":"2026-07-04T01:21:13","modified_gmt":"2026-07-04T01:21:13","slug":"16-python-anonymous-functions","status":"publish","type":"post","link":"https:\/\/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:\/\/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<p><!-- ktg-lesson-nav --><\/p>\n<nav class=\"ktg-lesson-nav\" aria-label=\"Lesson navigation\">\n<p><strong>Previous:<\/strong> <a href=\"https:\/\/kindsonthegenius.com\/python\/15-python-functions\/\">Lesson 14: Python &#8211; Functions<\/a><\/p>\n<p><strong>Next:<\/strong> <a href=\"https:\/\/kindsonthegenius.com\/python\/17-python-modules\/\">Lesson 16: Python &#8211; Modules<\/a><\/p>\n<\/nav>\n<p><!-- ktg-alkademy-cta --><\/p>\n<div class=\"ktg-alkademy-cta\" style=\"margin:2em 0;padding:1.25em;border-left:4px solid #2563eb;background:#f8fafc;\">\n<p><strong>Want live Python classes?<\/strong> Join <a href=\"https:\/\/www.alkademy.com\/courses\" target=\"_blank\" rel=\"noopener noreferrer\">Alkademy<\/a> for instructor-led Python and data science courses with hands-on projects.<\/p>\n<\/div>\n<p><!-- ktg-author-trust --><\/p>\n<div class=\"ktg-author-trust\" style=\"margin:2em 0;padding:1.25em;border-top:1px solid #e2e8f0;\">\n<p>Kindson Munonye is a software engineer and technical author specializing in Python, data science, and machine learning. He publishes free step-by-step Python tutorials and live classes on Alkademy.<\/p>\n<p><a href=\"https:\/\/github.com\/KindsonTheGenius\" target=\"_blank\" rel=\"noopener noreferrer\">GitHub<\/a> \u00b7 <a href=\"https:\/\/www.linkedin.com\/in\/kindson\" target=\"_blank\" rel=\"noopener noreferrer\">LinkedIn<\/a> \u00b7 <a href=\"https:\/\/www.kindsonthegenius.com\/about\/\">About<\/a> \u00b7 <a href=\"https:\/\/www.alkademy.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Alkademy<\/a><\/p>\n<\/div>\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,"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"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/python\/wp-json\/wp\/v2\/posts\/191","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/python\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/python\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/python\/wp-json\/wp\/v2\/users\/395"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/python\/wp-json\/wp\/v2\/comments?post=191"}],"version-history":[{"count":4,"href":"https:\/\/kindsonthegenius.com\/python\/wp-json\/wp\/v2\/posts\/191\/revisions"}],"predecessor-version":[{"id":581,"href":"https:\/\/kindsonthegenius.com\/python\/wp-json\/wp\/v2\/posts\/191\/revisions\/581"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/python\/wp-json\/wp\/v2\/media\/194"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/python\/wp-json\/wp\/v2\/media?parent=191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/python\/wp-json\/wp\/v2\/categories?post=191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/python\/wp-json\/wp\/v2\/tags?post=191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}