Skip to content

Commit cf2cc3b

Browse files
committed
function / generators / decorators lesson added
1 parent 87dd2df commit cf2cc3b

8 files changed

+1150
-8
lines changed
Lines changed: 296 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,300 @@
11
{
2-
"cells": [],
3-
"metadata": {},
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"### 1. WHILE STATEMENT"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": 1,
13+
"metadata": {},
14+
"outputs": [
15+
{
16+
"name": "stdout",
17+
"output_type": "stream",
18+
"text": [
19+
"counter is 1\n",
20+
"counter is 2\n",
21+
"counter is 3\n",
22+
"counter is 4\n",
23+
"counter is 5\n",
24+
"counter is 6\n",
25+
"counter is 7\n",
26+
"counter is 8\n"
27+
]
28+
}
29+
],
30+
"source": [
31+
"done = False\n",
32+
"counter = 1\n",
33+
"\n",
34+
"while counter <= 10 and not done:\n",
35+
" print('counter is', counter)\n",
36+
" if counter == 8:\n",
37+
" done = True\n",
38+
" counter += 1"
39+
]
40+
},
41+
{
42+
"cell_type": "markdown",
43+
"metadata": {},
44+
"source": [
45+
"### 2. FOR STATEMENT"
46+
]
47+
},
48+
{
49+
"cell_type": "code",
50+
"execution_count": 2,
51+
"metadata": {},
52+
"outputs": [
53+
{
54+
"name": "stdout",
55+
"output_type": "stream",
56+
"text": [
57+
"23\n",
58+
"45\n",
59+
"67\n",
60+
"89\n"
61+
]
62+
}
63+
],
64+
"source": [
65+
"for item in [23,45,67,89]:\n",
66+
" print(item)"
67+
]
68+
},
69+
{
70+
"cell_type": "markdown",
71+
"metadata": {},
72+
"source": [
73+
"#### NOTE:\n",
74+
" - If you need to access index of given data, you can use enumerate"
75+
]
76+
},
77+
{
78+
"cell_type": "code",
79+
"execution_count": null,
80+
"metadata": {
81+
"collapsed": true
82+
},
83+
"outputs": [],
84+
"source": []
85+
},
86+
{
87+
"cell_type": "code",
88+
"execution_count": 5,
89+
"metadata": {},
90+
"outputs": [
91+
{
92+
"name": "stdout",
93+
"output_type": "stream",
94+
"text": [
95+
"value at index 0 is 23\n",
96+
"value at index 1 is 45\n",
97+
"value at index 2 is 67\n",
98+
"value at index 3 is 89\n"
99+
]
100+
}
101+
],
102+
"source": [
103+
"for index, item in enumerate([23, 45,67,89]):\n",
104+
" print('value at index %s is %s'%( index, item))"
105+
]
106+
},
107+
{
108+
"cell_type": "markdown",
109+
"metadata": {},
110+
"source": [
111+
"#### 2.1 ZIP\n",
112+
" - ```zip``` is a built-in function used for combining iterables"
113+
]
114+
},
115+
{
116+
"cell_type": "code",
117+
"execution_count": 9,
118+
"metadata": {},
119+
"outputs": [
120+
{
121+
"name": "stdout",
122+
"output_type": "stream",
123+
"text": [
124+
"('ricki', 34) <class 'tuple'>\n",
125+
"('dikki', 56) <class 'tuple'>\n"
126+
]
127+
}
128+
],
129+
"source": [
130+
"names = ['ricki', 'dikki', 'tikki', 'mikki']\n",
131+
"ages = [34,56]\n",
132+
"for i in zip(names, ages):\n",
133+
" print(i, type(i))"
134+
]
135+
},
136+
{
137+
"cell_type": "markdown",
138+
"metadata": {},
139+
"source": [
140+
"### 3. COMPREHENSION\n",
141+
" - filtering iterable with some condition to create new iterable"
142+
]
143+
},
144+
{
145+
"cell_type": "code",
146+
"execution_count": 13,
147+
"metadata": {},
148+
"outputs": [
149+
{
150+
"name": "stdout",
151+
"output_type": "stream",
152+
"text": [
153+
"[4, 256, 46656, 16777216]\n"
154+
]
155+
}
156+
],
157+
"source": [
158+
"# we need a square of a list in another list\n",
159+
"\n",
160+
"# TRADITIONAL WAY\n",
161+
"list_1 = [2,4,6,8]\n",
162+
"square_list = []\n",
163+
"for item in list_1:\n",
164+
" square_list.append(item**item)\n",
165+
"\n",
166+
"print(square_list)"
167+
]
168+
},
169+
{
170+
"cell_type": "code",
171+
"execution_count": 12,
172+
"metadata": {},
173+
"outputs": [
174+
{
175+
"name": "stdout",
176+
"output_type": "stream",
177+
"text": [
178+
"[4, 256, 46656, 16777216]\n"
179+
]
180+
}
181+
],
182+
"source": [
183+
"# USING LIST COMPREHENSION\n",
184+
"square_list_comp = [item**item for item in list_1]\n",
185+
"print(square_list_comp)"
186+
]
187+
},
188+
{
189+
"cell_type": "code",
190+
"execution_count": 20,
191+
"metadata": {},
192+
"outputs": [
193+
{
194+
"name": "stdout",
195+
"output_type": "stream",
196+
"text": [
197+
"{42, 22, 30}\n",
198+
"{11: 22, 21: 42, 15: 30}\n"
199+
]
200+
}
201+
],
202+
"source": [
203+
"# comprehension\n",
204+
"\n",
205+
"numbers = [11, 15, 21]\n",
206+
"\n",
207+
"doubles_set = {2 * number for number in numbers}\n",
208+
"print(doubles_set)\n",
209+
"\n",
210+
"# a dict comprehension which uses the number as the key and the doubled number as the value\n",
211+
"doubles_dict = {number: 2 * number for number in numbers}\n",
212+
"print(doubles_dict)\n",
213+
" \n",
214+
" "
215+
]
216+
},
217+
{
218+
"cell_type": "code",
219+
"execution_count": 11,
220+
"metadata": {},
221+
"outputs": [
222+
{
223+
"data": {
224+
"text/plain": [
225+
"{1: 1,\n",
226+
" 2: 4,\n",
227+
" 3: 27,\n",
228+
" 4: 256,\n",
229+
" 5: 3125,\n",
230+
" 6: 46656,\n",
231+
" 7: 823543,\n",
232+
" 8: 16777216,\n",
233+
" 9: 387420489,\n",
234+
" 10: 10000000000,\n",
235+
" 11: 285311670611,\n",
236+
" 12: 8916100448256,\n",
237+
" 13: 302875106592253,\n",
238+
" 14: 11112006825558016,\n",
239+
" 15: 437893890380859375}"
240+
]
241+
},
242+
"execution_count": 11,
243+
"metadata": {},
244+
"output_type": "execute_result"
245+
}
246+
],
247+
"source": [
248+
"\n",
249+
"b = {key: key**key for key in range(1,16)}\n",
250+
"b"
251+
]
252+
},
253+
{
254+
"cell_type": "markdown",
255+
"metadata": {},
256+
"source": [
257+
"Write a Python program to combine values in python list of dictionaries.\n",
258+
"\n",
259+
"Sample data: [{'item': 'item1', 'amount': 400}, {'item': 'item2', 'amount': 300}, {'item': 'item1', 'amount': 750}]\n",
260+
"Expected Output: Counter({'item1': 1150, 'item2': 300})\n"
261+
]
262+
},
263+
{
264+
"cell_type": "code",
265+
"execution_count": 26,
266+
"metadata": {},
267+
"outputs": [
268+
{
269+
"name": "stdout",
270+
"output_type": "stream",
271+
"text": [
272+
"{'item1': 1150, 'item2': 300}\n"
273+
]
274+
}
275+
],
276+
"source": []
277+
}
278+
],
279+
"metadata": {
280+
"kernelspec": {
281+
"display_name": "Python 3",
282+
"language": "python",
283+
"name": "python3"
284+
},
285+
"language_info": {
286+
"codemirror_mode": {
287+
"name": "ipython",
288+
"version": 3
289+
},
290+
"file_extension": ".py",
291+
"mimetype": "text/x-python",
292+
"name": "python",
293+
"nbconvert_exporter": "python",
294+
"pygments_lexer": "ipython3",
295+
"version": "3.5.2"
296+
}
297+
},
4298
"nbformat": 4,
5299
"nbformat_minor": 2
6300
}

0 commit comments

Comments
 (0)