{"id":493,"date":"2020-10-13T02:54:47","date_gmt":"2020-10-13T02:54:47","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=493"},"modified":"2025-03-26T14:56:44","modified_gmt":"2025-03-26T14:56:44","slug":"python-lambda-expressions","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-basics\/python-lambda-expressions\/","title":{"rendered":"Python Lambda Expressions"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about Python lambda expressions and how to use them to write anonymous functions.<\/p>\n\n\n\n<p>Sometimes, you need to write a simple <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-functions\/\">function<\/a> that has only one expression. However, you need to use this function once. Therefore, it is unnecessary to define that function for this purpose.<\/p>\n\n\n\n<p>That&#8217;s where the Python lambda expressions come into play.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='what-are-python-lambda-expressions'>What are Python lambda expressions <a href=\"#what-are-python-lambda-expressions\" class=\"anchor\" id=\"what-are-python-lambda-expressions\" title=\"Anchor for What are Python lambda expressions\">#<\/a><\/h2>\n\n\n\n<p>Python lambda expressions allow you to define anonymous functions.<\/p>\n\n\n\n<p>Anonymous functions are functions without names. The anonymous functions are useful when you need to use them once.<\/p>\n\n\n\n<p>A lambda expression typically contains one or more arguments, but it can have <strong>only one expression<\/strong>.<\/p>\n\n\n\n<p>The following shows the lambda expression syntax:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">lambda<\/span> parameters: expression<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>It&#8217;s equivalent to the following function without the <code>\"anonymous\"<\/code> name:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">anonymous<\/span><span class=\"hljs-params\">(parameters)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">return<\/span> expression<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='python-lambda-expression-examples'>Python lambda expression examples <a href=\"#python-lambda-expression-examples\" class=\"anchor\" id=\"python-lambda-expression-examples\" title=\"Anchor for Python lambda expression examples\">#<\/a><\/h2>\n\n\n\n<p>In Python, you can pass a function to another function or return a function from another function.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='functions-that-accept-a-function-example'>Functions that accept a function example <a href=\"#functions-that-accept-a-function-example\" class=\"anchor\" id=\"functions-that-accept-a-function-example\" title=\"Anchor for Functions that accept a function example\">#<\/a><\/h3>\n\n\n\n<p>The following defines a function called <code>get_full_name()<\/code> that format the full name from the first name and last name:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">get_full_name<\/span><span class=\"hljs-params\">(first_name, last_name, formatter)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">return<\/span> formatter(first_name, last_name)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>get_full_name()<\/code> function accepts three arguments:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First name (<code>first_name<\/code>)<\/li>\n\n\n\n<li>Last name (<code>last_name<\/code>)<\/li>\n\n\n\n<li>A function that will format the full name (<code>formatter<\/code>). In turn, the <code>formatter<\/code> function accepts two arguments first name and last name.<\/li>\n<\/ul>\n\n\n\n<p>The following defines two functions that return a full name from the first name and last name in different formats:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">first_last<\/span><span class=\"hljs-params\">(first_name, last_name)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">f\"<span class=\"hljs-subst\">{first_name}<\/span> <span class=\"hljs-subst\">{last_name}<\/span>\"<\/span>\n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">last_first<\/span><span class=\"hljs-params\">(first_name, last_name)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">f\"<span class=\"hljs-subst\">{last_name}<\/span>, <span class=\"hljs-subst\">{first_name}<\/span>\"<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>And this shows you how to call the <code>get_full_name()<\/code> function by passing the first name, last name, and <code>first_last<\/code> \/ <code>last_first<\/code> functions:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">full_name = get_full_name(<span class=\"hljs-string\">'John'<\/span>, <span class=\"hljs-string\">'Doe'<\/span>, first_last)\n<span class=\"hljs-keyword\">print<\/span>(full_name) <span class=\"hljs-comment\"># John Doe<\/span>\n\nfull_name = get_full_name(<span class=\"hljs-string\">'John'<\/span>, <span class=\"hljs-string\">'Doe'<\/span>, last_first)\n<span class=\"hljs-keyword\">print<\/span>(full_name) <span class=\"hljs-comment\">#  Doe, John<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Here&#8217;s the complete program:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">get_full_name<\/span><span class=\"hljs-params\">(first_name, last_name, formatter)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">return<\/span> formatter(first_name, last_name)\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">first_last<\/span><span class=\"hljs-params\">(first_name, last_name)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">f\"<span class=\"hljs-subst\">{first_name}<\/span> <span class=\"hljs-subst\">{last_name}<\/span>\"<\/span>\n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">last_first<\/span><span class=\"hljs-params\">(first_name, last_name)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">f\"<span class=\"hljs-subst\">{last_name}<\/span>, <span class=\"hljs-subst\">{first_name}<\/span>\"<\/span>\n\nfull_name = get_full_name(<span class=\"hljs-string\">'John'<\/span>, <span class=\"hljs-string\">'Doe'<\/span>, first_last)\nprint(full_name) <span class=\"hljs-comment\"># John Doe<\/span>\n\nfull_name = get_full_name(<span class=\"hljs-string\">'John'<\/span>, <span class=\"hljs-string\">'Doe'<\/span>, last_first)\nprint(full_name) <span class=\"hljs-comment\">#  Doe, John<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=ZGVmIGdldF9mdWxsX25hbWUoZmlyc3RfbmFtZSwgbGFzdF9uYW1lLCBmb3JtYXR0ZXIpOgogICAgcmV0dXJuIGZvcm1hdHRlcihmaXJzdF9uYW1lLCBsYXN0X25hbWUpCgpkZWYgZmlyc3RfbGFzdChmaXJzdF9uYW1lLCBsYXN0X25hbWUpOgogICAgcmV0dXJuIGYie2ZpcnN0X25hbWV9IHtsYXN0X25hbWV9IgoKCmRlZiBsYXN0X2ZpcnN0KGZpcnN0X25hbWUsIGxhc3RfbmFtZSk6CiAgICByZXR1cm4gZiJ7bGFzdF9uYW1lfSwge2ZpcnN0X25hbWV9IgoKZnVsbF9uYW1lID0gZ2V0X2Z1bGxfbmFtZSgnSm9obicsICdEb2UnLCBmaXJzdF9sYXN0KQpwcmludChmdWxsX25hbWUpICMgSm9obiBEb2UKCmZ1bGxfbmFtZSA9IGdldF9mdWxsX25hbWUoJ0pvaG4nLCAnRG9lJywgbGFzdF9maXJzdCkKcHJpbnQoZnVsbF9uYW1lKSAjICBEb2UsIEpvaG4%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">John Doe\nDoe, John<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Instead of defining the <code>first_last<\/code> and <code>last_first<\/code> functions, you can use lambda expressions.<\/p>\n\n\n\n<p>For example, you can express the <code>first_last<\/code> function using the following lambda expression:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">lambda<\/span> first_name,last_name: <span class=\"hljs-string\">f\"<span class=\"hljs-subst\">{first_name}<\/span> <span class=\"hljs-subst\">{last_name}<\/span>\"<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This lambda expression accepts two arguments and concatenates them into a formatted string in the order <code>first_name<\/code>, space, and <code>last_name<\/code>.<\/p>\n\n\n\n<p>And the following converts the <code>last_first<\/code> function using a lambda expression that returns the full name in the format: last name, space, and first name:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">lambda<\/span> first_name, last_name: <span class=\"hljs-string\">f\"<span class=\"hljs-subst\">{last_name}<\/span> <span class=\"hljs-subst\">{first_name}<\/span>\"<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>By using lambda expressions, you can call the <code>get_full_name()<\/code> function as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">get_full_name<\/span><span class=\"hljs-params\">(first_name, last_name, formatter)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">return<\/span> formatter(first_name, last_name)\n\n\nfull_name = get_full_name(\n    <span class=\"hljs-string\">'John'<\/span>,\n    <span class=\"hljs-string\">'Doe'<\/span>,\n    <span class=\"hljs-keyword\">lambda<\/span> first_name, last_name: <span class=\"hljs-string\">f\"<span class=\"hljs-subst\">{first_name}<\/span> <span class=\"hljs-subst\">{last_name}<\/span>\"<\/span>\n)\nprint(full_name)\n\nfull_name = get_full_name(\n    <span class=\"hljs-string\">'John'<\/span>,\n    <span class=\"hljs-string\">'Doe'<\/span>,\n    <span class=\"hljs-keyword\">lambda<\/span> first_name, last_name: <span class=\"hljs-string\">f\"<span class=\"hljs-subst\">{last_name}<\/span>, <span class=\"hljs-subst\">{first_name}<\/span>\"<\/span>\n)\nprint(full_name)\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=ZGVmIGdldF9mdWxsX25hbWUoZmlyc3RfbmFtZSwgbGFzdF9uYW1lLCBmb3JtYXR0ZXIpOgogICAgcmV0dXJuIGZvcm1hdHRlcihmaXJzdF9uYW1lLCBsYXN0X25hbWUpCgoKZnVsbF9uYW1lID0gZ2V0X2Z1bGxfbmFtZSgKICAgICdKb2huJywKICAgICdEb2UnLAogICAgbGFtYmRhIGZpcnN0X25hbWUsIGxhc3RfbmFtZTogZiJ7Zmlyc3RfbmFtZX0ge2xhc3RfbmFtZX0iCikKcHJpbnQoZnVsbF9uYW1lKQoKZnVsbF9uYW1lID0gZ2V0X2Z1bGxfbmFtZSgKICAgICdKb2huJywKICAgICdEb2UnLAogICAgbGFtYmRhIGZpcnN0X25hbWUsIGxhc3RfbmFtZTogZiJ7bGFzdF9uYW1lfSwge2ZpcnN0X25hbWV9IgopCnByaW50KGZ1bGxfbmFtZSk%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/strong><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">John Doe\nDoe, John<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='functions-that-return-a-function-example'>Functions that return a function example <a href=\"#functions-that-return-a-function-example\" class=\"anchor\" id=\"functions-that-return-a-function-example\" title=\"Anchor for Functions that return a function example\">#<\/a><\/h3>\n\n\n\n<p>The following <code>times()<\/code> function returns a function which is a lambda expression:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">times<\/span><span class=\"hljs-params\">(n)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">lambda<\/span> x: x * n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>And this example shows how to call the <code>times()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">times<\/span><span class=\"hljs-params\">(n)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">lambda<\/span> x: x * n\n\ndouble = times(<span class=\"hljs-number\">2<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Since the <code>times()<\/code> function returns a function, the <code>double<\/code> is also a function. To call it, you place parentheses like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">times<\/span><span class=\"hljs-params\">(n)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">lambda<\/span> x: x * n\n\ndouble = times(<span class=\"hljs-number\">2<\/span>)\n\nresult = double(<span class=\"hljs-number\">2<\/span>)\nprint(result)\n\nresult = double(<span class=\"hljs-number\">3<\/span>)\nprint(result)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=ZGVmIHRpbWVzKG4pOgogICAgcmV0dXJuIGxhbWJkYSB4OiB4ICogbgoKZG91YmxlID0gdGltZXMoMikKCnJlc3VsdCA9IGRvdWJsZSgyKQpwcmludChyZXN1bHQpCgpyZXN1bHQgPSBkb3VibGUoMykKcHJpbnQocmVzdWx0KQ%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-number\">4<\/span>\n<span class=\"hljs-number\">6<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following shows another example of using the <code>times()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">times<\/span><span class=\"hljs-params\">(n)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">lambda<\/span> x: x * n\n\ntriple = times(<span class=\"hljs-number\">3<\/span>)\n\nprint(triple(<span class=\"hljs-number\">2<\/span>))  <span class=\"hljs-comment\"># 6<\/span>\nprint(triple(<span class=\"hljs-number\">3<\/span>))  <span class=\"hljs-comment\"># 9<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=ZGVmIHRpbWVzKG4pOgogICAgcmV0dXJuIGxhbWJkYSB4OiB4ICogbgoKdHJpcGxlID0gdGltZXMoMykKCnByaW50KHRyaXBsZSgyKSkgICMgNgpwcmludCh0cmlwbGUoMykpICAjIDk%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='python-lambda-in-a-loop'>Python lambda in a loop <a href=\"#python-lambda-in-a-loop\" class=\"anchor\" id=\"python-lambda-in-a-loop\" title=\"Anchor for Python lambda in a loop\">#<\/a><\/h2>\n\n\n\n<p>See the following example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">callables = &#91;]\n<span class=\"hljs-keyword\">for<\/span> i in (<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>):\n    callables.append(lambda: i)\n\n<span class=\"hljs-keyword\">for<\/span> f in callables:\n    <span class=\"hljs-keyword\">print<\/span>(f())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=Y2FsbGFibGVzID0gW10KZm9yIGkgaW4gKDEsIDIsIDMpOgogICAgY2FsbGFibGVzLmFwcGVuZChsYW1iZGE6IGkpCgpmb3IgZiBpbiBjYWxsYWJsZXM6CiAgICBwcmludChmKCkp\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, define a list with the name callables.<\/li>\n\n\n\n<li>Second, iterate from 1 to 3, create a new lambda expression in each iteration, and add it to the callables list.<\/li>\n\n\n\n<li>Third, loop over the callables and call each function.<\/li>\n<\/ul>\n\n\n\n<p>The expected output will be:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">1\n2\n3<\/code><\/span><\/pre>\n\n\n<p>However, the program shows the following output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">3\n3\n3<\/code><\/span><\/pre>\n\n\n<p>The problem is that all the there lambda expressions reference the <code>i<\/code> variable, not the current value of <code>i<\/code>. When you call the lambda expressions, the value of the variable <code>i<\/code> is <code>3<\/code>.<\/p>\n\n\n\n<p>To fix this, you need to bind the <code>i<\/code> variable to each lambda expression at the time the lambda expression is created. One way to do it is to use the <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-default-parameters\/\">default argument<\/a>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">callables = &#91;]\n<span class=\"hljs-keyword\">for<\/span> i in (<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>):\n    callables.append(lambda a=i: a)\n\n<span class=\"hljs-keyword\">for<\/span> f in callables:\n    <span class=\"hljs-keyword\">print<\/span>(f())\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=Y2FsbGFibGVzID0gW10KZm9yIGkgaW4gKDEsIDIsIDMpOgogICAgY2FsbGFibGVzLmFwcGVuZChsYW1iZGEgYT1pOiBhKQoKZm9yIGYgaW4gY2FsbGFibGVzOgogICAgcHJpbnQoZigpKQ%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">1\n2\n3<\/code><\/span><\/pre>\n\n\n<p>In this example, the value of a is evaluated at the time the lambda expression is created. Therefore, the program returns the expected output.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use Python lambda expressions to create anonymous functions, which are functions without names.<\/li>\n\n\n\n<li>A lambda expression accepts one or more arguments, contains an expression, and returns the result of that expression.<\/li>\n\n\n\n<li>Use lambda expressions to pass anonymous functions to a function and return a function from another function.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=lambda-expression\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"493\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-lambda-expressions\/\"\n\t\t\t\tdata-post-title=\"Python Lambda Expressions\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"493\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-lambda-expressions\/\"\n\t\t\t\tdata-post-title=\"Python Lambda Expressions\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you&#8217;ll learn about Python lambda expressions and how to use them to write anonymous functions. Sometimes, you need to write a simple function that has only one expression. However, you need to use this function once. Therefore, it is unnecessary to define that function for this purpose. That&#8217;s where the Python [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":37,"menu_order":23,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-493","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/493","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=493"}],"version-history":[{"count":3,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/493\/revisions"}],"predecessor-version":[{"id":7056,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/493\/revisions\/7056"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/37"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=493"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}