{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# lambda expressions\n",
"\n",
"One of Python's 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 def
s. There is a key difference that makes lambda useful in specialized roles:\n",
"\n",
"**A 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 than 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",
"Let's slowly break down a lambda expression by deconstructing a function:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"def square(num):\n",
" result = num**2\n",
" return result"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"square(2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Continuing the breakdown:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def square(num):\n",
" return num**2"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"square(2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can actually write this in one line (although it would be bad style to do so)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"def square(num): return num**2"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"square(2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This is the form that a lambda expression intends to replicate. A lambda expression can then be written as:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
">"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lambda num: num**2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note how we get a function back. We can assign this function to a label:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"square = lambda num: num**2"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"square(2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And there you have it! The breakdown of a function into a lambda expression!\n",
"Lets see a few more examples:\n",
"\n",
"## Example 1\n",
"Check that a number is even:"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"even = lambda x: x%2==0"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"even(3)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"even(4)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 2\n",
"Grab first character of a string:"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"first = lambda s: s[0]"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'h'"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"first('hello')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 3\n",
"Reverse a string:"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"rev = lambda s: s[::-1]"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'olleh'"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"rev('hello')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 4\n",
"Just like a normal function, we can accept more than one argument into a lambda expression:"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"adder = lambda x,y : x+y"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"adder(2,3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"lambda expressions really shine when used in conjunction with **map()**, **filter()** and **reduce()**. Each of those functions has its own lecture, so feel free to explore them if you're very interested in lambda."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"I highly recommend reading this blog post at [Python Conquers the Universe](https://pythonconquerstheuniverse.wordpress.com/2011/08/29/lambda_tutorial/) for a great breakdown on lambda expressions and some explanations of common confusions! "
]
}
],
"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.6.2"
}
},
"nbformat": 4,
"nbformat_minor": 1
}