From ab0618da1cdf7b4471b52a0136e70166baf809b1 Mon Sep 17 00:00:00 2001
From: Tanu Nanda Prabhu <49044332+Tanu-N-Prabhu@users.noreply.github.com>
Date: Fri, 15 Nov 2019 12:48:12 -0600
Subject: [PATCH] Created using Colaboratory
---
Python_Lambda_Function.ipynb | 577 +++++++++++++++++++++++++++++++++++
1 file changed, 577 insertions(+)
create mode 100644 Python_Lambda_Function.ipynb
diff --git a/Python_Lambda_Function.ipynb b/Python_Lambda_Function.ipynb
new file mode 100644
index 0000000..40489ce
--- /dev/null
+++ b/Python_Lambda_Function.ipynb
@@ -0,0 +1,577 @@
+{
+ "nbformat": 4,
+ "nbformat_minor": 0,
+ "metadata": {
+ "colab": {
+ "name": "Python Lambda Function.ipynb",
+ "provenance": [],
+ "include_colab_link": true
+ },
+ "kernelspec": {
+ "name": "python3",
+ "display_name": "Python 3"
+ }
+ },
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "view-in-github",
+ "colab_type": "text"
+ },
+ "source": [
+ "
"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "8vnK3Zv7NrwR",
+ "colab_type": "text"
+ },
+ "source": [
+ "# Python Lambda Function"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "PVIR1mAON0us",
+ "colab_type": "text"
+ },
+ "source": [
+ "## In this article, you will learn more about lambda functions in python. How can we use lambda functions, what is the syntax of lambda functions and why do we need them?"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "b3h2Dg0-WAAp",
+ "colab_type": "text"
+ },
+ "source": [
+ ""
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "JWW8Q9VYWjwf",
+ "colab_type": "text"
+ },
+ "source": [
+ "# What are lambda functions and what are its characteristics?\n",
+ "\n",
+ "Lambda functions are also called ***anonymous functions***. An anonymous function is a function defined without a name. As you know to define a normal function in python, you need to use the `def` keyword. But in this case of anonymous functions, we use the `lambda` keyword to define the functions. Alonzo Church in the 1930s to the field of mathematics introduced the lambda functions."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "QF6yXverWzh-",
+ "colab_type": "text"
+ },
+ "source": [
+ "## The characteristics of lambda functions are:\n",
+ "\n",
+ "\n",
+ "1. The lambda function can take many arguments but can return only one expression. Here the expression is nothing but the result returned by the lambda function.\n",
+ "\n",
+ "2. Lambda functions are syntactically restricted to return a single expression.\n",
+ "\n",
+ "3. You can use them as an anonymous function inside other functions.\n",
+ "\n",
+ "4. The lambda functions do not need a return statement, they always return a single expression.\n",
+ "\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "qLCPMNTeXGGW",
+ "colab_type": "text"
+ },
+ "source": [
+ "\n",
+ "> ***Use lambda functions when an anonymous function is required for a short period of time. — I don’t why they say this, if you know, please let me know, I’m curious.***\n",
+ "\n",
+ "\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "O1dJLygcXMOA",
+ "colab_type": "text"
+ },
+ "source": [
+ "\n",
+ "\n",
+ "---\n",
+ "\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "cAzaa5HdXMyv",
+ "colab_type": "text"
+ },
+ "source": [
+ "# What is the syntax of lambda functions?\n",
+ "\n",
+ "**The syntax of lambda functions is shown below:**\n",
+ "\n",
+ "\n",
+ "\n",
+ "> ***lambda arguments: expression***\n",
+ "\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "9A9c4wCpXT5O",
+ "colab_type": "text"
+ },
+ "source": [
+ "**The expression is always executed and returned.**\n",
+ "\n",
+ "Lets us understand the working of lambda functions, we will just use simple logic to understand the working of lambda functions by adding two numbers. Firstly, I will write the program and then explain to you what it does?"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "DRuTR9W2XX1A",
+ "colab_type": "code",
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 35
+ },
+ "outputId": "1ded40fe-a523-4e15-b393-7719160ef9b4"
+ },
+ "source": [
+ "add = lambda a: a + a\n",
+ "print(add(20))"
+ ],
+ "execution_count": 1,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "text": [
+ "40\n"
+ ],
+ "name": "stdout"
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "_h1MUJoaXaiQ",
+ "colab_type": "text"
+ },
+ "source": [
+ "**Ok firstly did you understand anything from the program, don’t worry I will explain it to you step by step.**\n",
+ "\n",
+ "1. First, define the anonymous/ lambda function by using `lambda` keyword.\n",
+ "\n",
+ "2. Just remember the syntax of lambda functions which is as shown below: ***lambda arguments: expression***, then just use the variable “***a***” as an argument and “***a + a***” as the expression, remember we are adding two numbers (a + a).\n",
+ "\n",
+ "3. Then to store the final added result let us use a variable ***“add”***.\n",
+ "\n",
+ "4. And then just as calling a normal function, we shall call the lambda function with the name of its variable “***add***” by sending a parameter ***“20”*** to perform the addition and then print it “***print(add(20))***”.\n",
+ "\n",
+ "5. This is the final step, here when we print the result we then get ***40*** because of “***20+20***” since we are adding “***a+a***”. Look here I’m not using the return statement.\n",
+ "\n",
+ "\n",
+ "\n",
+ "---\n",
+ "\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "pMObcbHJX51k",
+ "colab_type": "text"
+ },
+ "source": [
+ "Now that we know how lambda function works its time to play with it. ***Lambda function takes multiple arguments, let us see if this statement is true or not.***"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "ViEjE0bAX9we",
+ "colab_type": "code",
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 35
+ },
+ "outputId": "94f79da1-3e4c-47b2-bc6b-1ebb99626ac8"
+ },
+ "source": [
+ "add = lambda a, b, c: a + b + c\n",
+ "print(add(10, 10, 10))"
+ ],
+ "execution_count": 2,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "text": [
+ "30\n"
+ ],
+ "name": "stdout"
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "kR7jVlbiYGuP",
+ "colab_type": "text"
+ },
+ "source": [
+ "Wow, the above statement is actually true, because I passed more than one argument to the lambda function and then it worked.\n",
+ "\n",
+ "\n",
+ "\n",
+ "---\n",
+ "\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "vYtT1VqvYHy0",
+ "colab_type": "text"
+ },
+ "source": [
+ "# What is the difference between lambda functions and normal functions in python?\n",
+ "\n",
+ "\n",
+ "This is one of the hardest questions to answer, after doing some research I nearly came to answer the above question. I don't want to make general assumptions rather I would like to answer this question with the help of an example:\n",
+ "\n",
+ "**Example 1: Adding three numbers using the normal function.**"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "dV-f8U4vYNr5",
+ "colab_type": "code",
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 35
+ },
+ "outputId": "ecaca037-7047-4d14-a676-17b7d260c7ae"
+ },
+ "source": [
+ "def add(a,b,c):\n",
+ " return a+b+c\n",
+ "result = add(10, 10, 10)\n",
+ "print(result)"
+ ],
+ "execution_count": 4,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "text": [
+ "30\n"
+ ],
+ "name": "stdout"
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "brbsZATxYOzB",
+ "colab_type": "code",
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 35
+ },
+ "outputId": "a6726f56-dbf5-4ae9-8fe9-b866be84c621"
+ },
+ "source": [
+ "print(type(result))"
+ ],
+ "execution_count": 5,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "text": [
+ "\n"
+ ],
+ "name": "stdout"
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "qLtLLO0WYTGL",
+ "colab_type": "text"
+ },
+ "source": [
+ "**Example 2: Adding three numbers using the lambda function.**"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "nlIx3VrMYXC3",
+ "colab_type": "code",
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 35
+ },
+ "outputId": "56780cfd-663f-495f-d6b6-fde3a95de05c"
+ },
+ "source": [
+ "add = lambda a, b, c: a + b + c\n",
+ "print(add(10, 10, 10))"
+ ],
+ "execution_count": 6,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "text": [
+ "30\n"
+ ],
+ "name": "stdout"
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "TvqJjqK-YYqd",
+ "colab_type": "code",
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 35
+ },
+ "outputId": "a359c1ef-3430-4837-fb1b-b1901658232f"
+ },
+ "source": [
+ "print(type(add))"
+ ],
+ "execution_count": 7,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "text": [
+ "\n"
+ ],
+ "name": "stdout"
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "iuIc3McUYVTd",
+ "colab_type": "text"
+ },
+ "source": [
+ "Based on the above two examples I have made a few differences between the normal function and the lambda function. I hope it is helpful.\n",
+ "\n",
+ ""
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "WszoCx3pYg-c",
+ "colab_type": "text"
+ },
+ "source": [
+ "You are wondering how did I calculate the execution time of the program. Its simple I just used the ***time*** library. Alright, I will provide you the code below."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "vgedmpwOYj6h",
+ "colab_type": "code",
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 52
+ },
+ "outputId": "bfe25efc-005b-453a-961d-835539546a39"
+ },
+ "source": [
+ "import time\n",
+ "start_time = time.time()\n",
+ "add = lambda a, b, c: a + b + c\n",
+ "print(add(10, 10, 10))\n",
+ "print(\"--- %s seconds ---\" % (time.time() - start_time))"
+ ],
+ "execution_count": 8,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "text": [
+ "30\n",
+ "--- 0.001165151596069336 seconds ---\n"
+ ],
+ "name": "stdout"
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "_9tJQOZrYmmc",
+ "colab_type": "text"
+ },
+ "source": [
+ "\n",
+ "\n",
+ "---\n",
+ "\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "uNWQBvDYYnMQ",
+ "colab_type": "text"
+ },
+ "source": [
+ "# Why should we use lambda functions?\n",
+ "\n",
+ "We can use lambda functions ***as anonymous functions inside any normal functions of python***. This is the actual superpower of lambda functions. Because of its ***simplicity***, you can write a lambda function with no hassle. Now just think you need to write a normal function of adding every number and the current given number you have given.\n",
+ "\n",
+ "\n",
+ "\n",
+ "> Confused, OK imagine now you have the number 100 as constant and every time you need to add another number such as 200, 300, 400,…. to the existing number. Here, rather than defining another function inside the same function, you use the lambda function as shown below.\n",
+ "\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "IAzniw2TYzKU",
+ "colab_type": "code",
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 35
+ },
+ "outputId": "d71f8a26-02f2-4389-e6d1-aacabaa32dea"
+ },
+ "source": [
+ "def add(n):\n",
+ " return lambda a : a + n\n",
+ "result = add(100)\n",
+ "print(result(200))"
+ ],
+ "execution_count": 9,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "text": [
+ "300\n"
+ ],
+ "name": "stdout"
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "qRJzcR5OYw6G",
+ "colab_type": "text"
+ },
+ "source": [
+ "You can get the job done in just one step of using a lambda function. Like I said the ***simplicity and the usability*** inside another function are the prime features of a lambda function compared to the normal functions.\n",
+ "\n",
+ "\n",
+ "\n",
+ "---\n",
+ "\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "cFIExobsY5lZ",
+ "colab_type": "text"
+ },
+ "source": [
+ "# Can we reuse the lambda function?\n",
+ "\n",
+ "I guess the answer is yes because in the below example I can reuse the same lambda function to add the number to the existing numbers."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "lQ6sDlraY8Er",
+ "colab_type": "code",
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 139
+ },
+ "outputId": "403935a7-a129-43ef-e6f4-0861f25790c2"
+ },
+ "source": [
+ "def add(n):\n",
+ " return lambda a : a + n\n",
+ "result = add(100)\n",
+ "print(result(200))\n",
+ "print(\"-------------------\")\n",
+ "print(result(300))\n",
+ "print(\"-------------------\")\n",
+ "print(result(400))\n",
+ "print(\"-------------------\")\n",
+ "print(result(500))"
+ ],
+ "execution_count": 10,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "text": [
+ "300\n",
+ "-------------------\n",
+ "400\n",
+ "-------------------\n",
+ "500\n",
+ "-------------------\n",
+ "600\n"
+ ],
+ "name": "stdout"
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "WOYB1KAGY9-E",
+ "colab_type": "text"
+ },
+ "source": [
+ "\n",
+ "\n",
+ "---\n",
+ "\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "6qPQbhNeY-Z6",
+ "colab_type": "text"
+ },
+ "source": [
+ "Well, there you go you would have now got a better understanding of lambda functions. This is the end of the tutorial of “***Python Lambda Functions***”, there are more important concepts such as maps, filters and many more. These are altogether different concepts, I shall write them as separate tutorials shortly. Stay tuned!!. Until then see you, have a nice day.\n",
+ "\n",
+ "# Author: Tanu Nanda Prabhu"
+ ]
+ }
+ ]
+}
\ No newline at end of file