{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "A lambda function: \n", "* is a small anonymous function.\n", "* can take any number of arguments\n", "* can only have one expression.\n", "\n", "sintaxe:\n", "\n", " lambda arguments : expression" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. Exemplo with one argument" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = lambda x : x + 20" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(a(2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2. Non lambda function alternative" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def aAlt(x):\n", " return x+20" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(aAlt(2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3. Exemplo with one argument" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\n", "b = lambda x, y : x * y\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(b(10,20)) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "4. Non lambda function alternative" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# alterantive\n", "def bAlt(x,y):\n", " return x*y" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(bAlt(10,20)) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The power of lambda is better shown when you use them as an anonymous function inside another function.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "5. We have function definition that takes one argument, and that argument will be multiplied with an unknown number." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def myFunc(n):\n", " return lambda a : a * n " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "doubling = myFunc(2)\n", "tripling = myFunc(3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(tripling(11))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "6. The filter function is used to select some particular elements from a sequence of elements. \n", "The sequence can be any iterator like lists, sets, tuples, etc. lambdas in filter()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sequences = [2,3,6,7,1,1,8]\n", "filteredResult = filter (lambda x: x > 4, sequences) \n", "list(filteredResult)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "7. the map function is used to apply a particular operation to every element in a sequence. Like filter(), it also takes 2 parameters: a function that defines the operation to perform on the elements, and one or more sequences. The follwoing program prints the squares of numbers in the previous list: " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "filteredResult1 = map (lambda x: x**2, sequences) \n", "print(list(filteredResult1))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.0" } }, "nbformat": 4, "nbformat_minor": 2 }