{ "cells": [ { "cell_type": "markdown", "id": "76b3bfdb", "metadata": {}, "source": [ "# [Lambda Functions](https://realpython.com/python-lambda/)\n", "\n", "One of Pythons most useful (and for beginners, confusing) tools is the lambda expression. lambda expressions allow us to create \"anonymous\" functions. This basically means we can quickly make ad-hoc functions without needing to properly define a function using def.\n", "\n", "Function objects returned by running lambda expressions work exactly the same as those created and assigned by defs. There is key difference that makes lambda useful in specialized roles:\n", "\n", "**lambda's body is a single expression, not a block of statements.**\n", "\n", "- The lambda's body is similar to what we would put in a def body's return statement. We simply type the result as an expression instead of explicitly returning it. Because it is limited to an expression, a lambda is less general that a def. We can only squeeze design, to limit program nesting. lambda is designed for coding simple functions, and def handles the larger tasks.\n", "\n", "Lets slowly break down a lambda expression by deconstructing a function:" ] }, { "cell_type": "code", "execution_count": 1, "id": "085c18b4", "metadata": {}, "outputs": [], "source": [ "def square(num): return num ** 2" ] }, { "cell_type": "code", "execution_count": 2, "id": "2ff8537f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "square(3)" ] }, { "cell_type": "code", "execution_count": 3, "id": "de12df61", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(num)>" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lambda num : num ** 2" ] }, { "cell_type": "code", "execution_count": 4, "id": "17183833", "metadata": {}, "outputs": [], "source": [ "square = lambda num : num ** 2" ] }, { "cell_type": "code", "execution_count": 5, "id": "fc601057", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "25" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "square(5)" ] }, { "cell_type": "markdown", "id": "8dda9fa7", "metadata": {}, "source": [ "Many function calls need a function passed in, such as map and filter. Often you only need to use the function you are passing in once, so instead of formally defining it, you just use the lambda expression. Let's repeat some of the examples from above with a lambda expression" ] }, { "cell_type": "code", "execution_count": 6, "id": "113c0f4d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "nums = [i for i in range(11)]\n", "list(map(lambda x: x ** 2, nums))" ] }, { "cell_type": "code", "execution_count": 7, "id": "be2fa373", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 2, 4, 6, 8, 10]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(filter(lambda x : x % 2 == 0, nums))" ] }, { "cell_type": "markdown", "id": "f15dc2fe", "metadata": {}, "source": [ "**Lambda expression for grabbing the first character of a string:**" ] }, { "cell_type": "code", "execution_count": 8, "id": "2b77a01c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(s)>" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lambda s : s[0]" ] }, { "cell_type": "markdown", "id": "6aeec790", "metadata": {}, "source": [ "**Lambda expression for reversing a string:**" ] }, { "cell_type": "code", "execution_count": 9, "id": "87ecf1eb", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(s)>" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lambda s : s[::-1]" ] }, { "cell_type": "markdown", "id": "7a9b5c4a", "metadata": {}, "source": [ "**You can even pass in multiple arguments into a lambda expression.**" ] }, { "cell_type": "code", "execution_count": 10, "id": "b7ac7419", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(x, y)>" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lambda x, y : x + y" ] } ], "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.8.8" } }, "nbformat": 4, "nbformat_minor": 5 }