Skip to content

Commit 08aca35

Browse files
committed
Lecture 4 references addeD
1 parent 2bcb686 commit 08aca35

20 files changed

+3167
-0
lines changed

.DS_Store

-6 KB
Binary file not shown.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
*.sw[poc]
2+
.DS_Store
23
*.py[poc]
34

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# <center>Intermediate Python (Part-1)</center>"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"# ***<center>Iterators and Generators</center>***\n",
15+
"\n",
16+
"<img src=https://i.imgur.com/e1Deq4a.jpg height=300 width=300>"
17+
]
18+
},
19+
{
20+
"cell_type": "markdown",
21+
"metadata": {},
22+
"source": [
23+
"## 1. Iteration protocol in Python\n",
24+
"\n",
25+
"\n",
26+
"- **Iteration:** repitition of a process.\n",
27+
"- **Iterable:** a Python object which supports iteration.\n",
28+
"- **Iterator:** a Python object to perform iteration over an iterable."
29+
]
30+
},
31+
{
32+
"cell_type": "markdown",
33+
"metadata": {},
34+
"source": [
35+
"![](http://nvie.com/img/iterable-vs-iterator.png)"
36+
]
37+
},
38+
{
39+
"cell_type": "markdown",
40+
"metadata": {},
41+
"source": [
42+
"### Iteration Protocol in Python\n",
43+
"\n",
44+
"The **iteration protocol** is a fancy term meaning “how iterables actually work in Python”.\n",
45+
"\n",
46+
"1. For a class object to be an Iterable:\n",
47+
" - Can be passed to the iter function to get an iterator for them.\n",
48+
"\n",
49+
"2. For any Iterator:\n",
50+
" - Can be passed to the next function which gives their next item or raises StopIteration\n",
51+
" - Return themselves when passed to the iter function."
52+
]
53+
},
54+
{
55+
"cell_type": "markdown",
56+
"metadata": {},
57+
"source": [
58+
"![](https://image.slidesharecdn.com/pythonadvanced-151127114045-lva1-app6891/95/python-advanced-building-on-the-foundation-102-638.jpg?cb=1448910770)"
59+
]
60+
},
61+
{
62+
"cell_type": "code",
63+
"execution_count": null,
64+
"metadata": {},
65+
"outputs": [],
66+
"source": []
67+
},
68+
{
69+
"cell_type": "markdown",
70+
"metadata": {},
71+
"source": [
72+
"## 2. Generators\n",
73+
"\n",
74+
"Simple **functions** or **expressions** used to create iterator.\n",
75+
"\n",
76+
"Let's write a function which return the factorial of first 10 natural numbers."
77+
]
78+
},
79+
{
80+
"cell_type": "code",
81+
"execution_count": 22,
82+
"metadata": {},
83+
"outputs": [],
84+
"source": [
85+
"def factorial(n):\n",
86+
" fact = []\n",
87+
" k = 1\n",
88+
" for i in range(1,n+1):\n",
89+
" k *= i\n",
90+
" fact.append(k)\n",
91+
" return fact"
92+
]
93+
},
94+
{
95+
"cell_type": "markdown",
96+
"metadata": {},
97+
"source": [
98+
"Let's make it memory efficient using generators!"
99+
]
100+
},
101+
{
102+
"cell_type": "markdown",
103+
"metadata": {},
104+
"source": [
105+
"![](https://paulohrpinheiro.xyz/texts/python/images/lazy-evaluation.jpg)"
106+
]
107+
},
108+
{
109+
"cell_type": "markdown",
110+
"metadata": {},
111+
"source": [
112+
"![](http://nvie.com/img/relationships.png)"
113+
]
114+
},
115+
{
116+
"cell_type": "markdown",
117+
"metadata": {},
118+
"source": [
119+
"### Generator expression\n",
120+
"\n",
121+
"Now, let us find the sum of squares of first 10 natural numbers, but this time, without any function!"
122+
]
123+
},
124+
{
125+
"cell_type": "code",
126+
"execution_count": null,
127+
"metadata": {},
128+
"outputs": [],
129+
"source": []
130+
},
131+
{
132+
"cell_type": "markdown",
133+
"metadata": {},
134+
"source": [
135+
"This can also be converted into a generator **expression**!"
136+
]
137+
},
138+
{
139+
"cell_type": "code",
140+
"execution_count": null,
141+
"metadata": {},
142+
"outputs": [],
143+
"source": []
144+
},
145+
{
146+
"cell_type": "markdown",
147+
"metadata": {},
148+
"source": [
149+
"# Let's sum up it all!\n",
150+
"![](https://raw.github.com/wardi/iterables-iterators-generators/master/iterable_iterator_generator.png)"
151+
]
152+
}
153+
],
154+
"metadata": {
155+
"kernelspec": {
156+
"display_name": "Python 3",
157+
"language": "python",
158+
"name": "python3"
159+
},
160+
"language_info": {
161+
"codemirror_mode": {
162+
"name": "ipython",
163+
"version": 3
164+
},
165+
"file_extension": ".py",
166+
"mimetype": "text/x-python",
167+
"name": "python",
168+
"nbconvert_exporter": "python",
169+
"pygments_lexer": "ipython3",
170+
"version": "3.6.3"
171+
}
172+
},
173+
"nbformat": 4,
174+
"nbformat_minor": 2
175+
}

0 commit comments

Comments
 (0)