Skip to content

Commit 2f657cf

Browse files
committed
Adding Lambda Lab intro
1 parent 540462f commit 2f657cf

File tree

1 file changed

+256
-0
lines changed

1 file changed

+256
-0
lines changed

notebooks/Lambda_Lab.ipynb

Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"## What is lambda? \n",
8+
"\n",
9+
"- A shortcut for building one-off functions\n",
10+
"- Define functions in-line"
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": 1,
16+
"metadata": {
17+
"collapsed": true
18+
},
19+
"outputs": [],
20+
"source": [
21+
"# Traditional function\n",
22+
"\n",
23+
"import math\n",
24+
"def square_root(x):\n",
25+
" return math.sqrt(x)"
26+
]
27+
},
28+
{
29+
"cell_type": "code",
30+
"execution_count": 5,
31+
"metadata": {
32+
"collapsed": true
33+
},
34+
"outputs": [],
35+
"source": [
36+
"# using lambda\n",
37+
"\n",
38+
"square_rt = lambda x: math.sqrt(x)"
39+
]
40+
},
41+
{
42+
"cell_type": "code",
43+
"execution_count": 4,
44+
"metadata": {
45+
"collapsed": false
46+
},
47+
"outputs": [
48+
{
49+
"data": {
50+
"text/plain": [
51+
"5.0"
52+
]
53+
},
54+
"execution_count": 4,
55+
"metadata": {},
56+
"output_type": "execute_result"
57+
}
58+
],
59+
"source": [
60+
"square_root(25)"
61+
]
62+
},
63+
{
64+
"cell_type": "code",
65+
"execution_count": 6,
66+
"metadata": {
67+
"collapsed": false
68+
},
69+
"outputs": [
70+
{
71+
"data": {
72+
"text/plain": [
73+
"5.0"
74+
]
75+
},
76+
"execution_count": 6,
77+
"metadata": {},
78+
"output_type": "execute_result"
79+
}
80+
],
81+
"source": [
82+
"square_rt(25)"
83+
]
84+
},
85+
{
86+
"cell_type": "markdown",
87+
"metadata": {},
88+
"source": [
89+
"## When should you use lambda? "
90+
]
91+
},
92+
{
93+
"cell_type": "markdown",
94+
"metadata": {},
95+
"source": [
96+
"- Functions that should only be used once\n",
97+
"- Code that only performs one, well-defined task\n",
98+
"- Passing a function to other functions\n",
99+
"- Cleaner code, avoid duplication"
100+
]
101+
},
102+
{
103+
"cell_type": "code",
104+
"execution_count": 14,
105+
"metadata": {
106+
"collapsed": false
107+
},
108+
"outputs": [
109+
{
110+
"data": {
111+
"text/plain": [
112+
"25"
113+
]
114+
},
115+
"execution_count": 14,
116+
"metadata": {},
117+
"output_type": "execute_result"
118+
}
119+
],
120+
"source": [
121+
"# traditional function\n",
122+
"def square(x):\n",
123+
" return x**2\n",
124+
"square(5)"
125+
]
126+
},
127+
{
128+
"cell_type": "code",
129+
"execution_count": 15,
130+
"metadata": {
131+
"collapsed": false
132+
},
133+
"outputs": [
134+
{
135+
"data": {
136+
"text/plain": [
137+
"25"
138+
]
139+
},
140+
"execution_count": 15,
141+
"metadata": {},
142+
"output_type": "execute_result"
143+
}
144+
],
145+
"source": [
146+
"# lambda\n",
147+
"square = lambda x: x**2\n",
148+
"square(5)"
149+
]
150+
},
151+
{
152+
"cell_type": "code",
153+
"execution_count": 16,
154+
"metadata": {
155+
"collapsed": false
156+
},
157+
"outputs": [
158+
{
159+
"data": {
160+
"text/plain": [
161+
"[25]"
162+
]
163+
},
164+
"execution_count": 16,
165+
"metadata": {},
166+
"output_type": "execute_result"
167+
}
168+
],
169+
"source": [
170+
"# list comprehension\n",
171+
"a = [5]\n",
172+
"[x**2 for x in a]"
173+
]
174+
},
175+
{
176+
"cell_type": "markdown",
177+
"metadata": {},
178+
"source": [
179+
"## Lambda Lab"
180+
]
181+
},
182+
{
183+
"cell_type": "code",
184+
"execution_count": 17,
185+
"metadata": {
186+
"collapsed": true
187+
},
188+
"outputs": [],
189+
"source": [
190+
"# write a function\n",
191+
"def function_builder():\n",
192+
" pass"
193+
]
194+
},
195+
{
196+
"cell_type": "code",
197+
"execution_count": null,
198+
"metadata": {
199+
"collapsed": true
200+
},
201+
"outputs": [],
202+
"source": [
203+
"# return a list of n functions\n",
204+
"\n",
205+
"def function_builder(n):\n",
206+
" func_list = []\n",
207+
" for i in range(n):\n",
208+
" func_list.append(insert lambda function here)\n",
209+
" return (func_list)"
210+
]
211+
},
212+
{
213+
"cell_type": "code",
214+
"execution_count": null,
215+
"metadata": {
216+
"collapsed": true
217+
},
218+
"outputs": [],
219+
"source": [
220+
"# fill in lambda function"
221+
]
222+
},
223+
{
224+
"cell_type": "code",
225+
"execution_count": null,
226+
"metadata": {
227+
"collapsed": true
228+
},
229+
"outputs": [],
230+
"source": [
231+
"# update function to use list comprehension"
232+
]
233+
}
234+
],
235+
"metadata": {
236+
"kernelspec": {
237+
"display_name": "Python 3",
238+
"language": "python",
239+
"name": "python3"
240+
},
241+
"language_info": {
242+
"codemirror_mode": {
243+
"name": "ipython",
244+
"version": 3
245+
},
246+
"file_extension": ".py",
247+
"mimetype": "text/x-python",
248+
"name": "python",
249+
"nbconvert_exporter": "python",
250+
"pygments_lexer": "ipython3",
251+
"version": "3.5.0"
252+
}
253+
},
254+
"nbformat": 4,
255+
"nbformat_minor": 0
256+
}

0 commit comments

Comments
 (0)