Skip to content

Commit 67415fc

Browse files
authored
Add files via upload
1 parent b7ad27a commit 67415fc

File tree

4 files changed

+924
-0
lines changed

4 files changed

+924
-0
lines changed
Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"### Iterators\n",
8+
"Iterators are advanced Python concepts that allow for efficient looping and memory management. Iterators provide a way to access elements of a collection sequentially without exposing the underlying structure.\n"
9+
]
10+
},
11+
{
12+
"cell_type": "code",
13+
"execution_count": 6,
14+
"metadata": {},
15+
"outputs": [
16+
{
17+
"name": "stdout",
18+
"output_type": "stream",
19+
"text": [
20+
"1\n",
21+
"2\n",
22+
"3\n",
23+
"4\n",
24+
"5\n",
25+
"6\n"
26+
]
27+
}
28+
],
29+
"source": [
30+
"my_list=[1,2,3,4,5,6]\n",
31+
"for i in my_list:\n",
32+
" print(i)"
33+
]
34+
},
35+
{
36+
"cell_type": "code",
37+
"execution_count": 7,
38+
"metadata": {},
39+
"outputs": [
40+
{
41+
"data": {
42+
"text/plain": [
43+
"list"
44+
]
45+
},
46+
"execution_count": 7,
47+
"metadata": {},
48+
"output_type": "execute_result"
49+
}
50+
],
51+
"source": [
52+
"type(my_list)"
53+
]
54+
},
55+
{
56+
"cell_type": "code",
57+
"execution_count": 8,
58+
"metadata": {},
59+
"outputs": [
60+
{
61+
"name": "stdout",
62+
"output_type": "stream",
63+
"text": [
64+
"[1, 2, 3, 4, 5, 6]\n"
65+
]
66+
}
67+
],
68+
"source": [
69+
"print(my_list)"
70+
]
71+
},
72+
{
73+
"cell_type": "code",
74+
"execution_count": 9,
75+
"metadata": {},
76+
"outputs": [
77+
{
78+
"name": "stdout",
79+
"output_type": "stream",
80+
"text": [
81+
"<class 'list_iterator'>\n"
82+
]
83+
}
84+
],
85+
"source": [
86+
"## Iterator\n",
87+
"iterator=iter(my_list)\n",
88+
"print(type(iterator))"
89+
]
90+
},
91+
{
92+
"cell_type": "code",
93+
"execution_count": 10,
94+
"metadata": {},
95+
"outputs": [
96+
{
97+
"data": {
98+
"text/plain": [
99+
"<list_iterator at 0x27a325efb20>"
100+
]
101+
},
102+
"execution_count": 10,
103+
"metadata": {},
104+
"output_type": "execute_result"
105+
}
106+
],
107+
"source": [
108+
"iterator"
109+
]
110+
},
111+
{
112+
"cell_type": "code",
113+
"execution_count": 17,
114+
"metadata": {},
115+
"outputs": [
116+
{
117+
"ename": "StopIteration",
118+
"evalue": "",
119+
"output_type": "error",
120+
"traceback": [
121+
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
122+
"\u001b[1;31mStopIteration\u001b[0m Traceback (most recent call last)",
123+
"Cell \u001b[1;32mIn[17], line 3\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m## Iterate through all the element\u001b[39;00m\n\u001b[1;32m----> 3\u001b[0m \u001b[38;5;28;43mnext\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43miterator\u001b[49m\u001b[43m)\u001b[49m\n",
124+
"\u001b[1;31mStopIteration\u001b[0m: "
125+
]
126+
}
127+
],
128+
"source": [
129+
"## Iterate through all the element\n",
130+
"\n",
131+
"next(iterator)"
132+
]
133+
},
134+
{
135+
"cell_type": "code",
136+
"execution_count": 18,
137+
"metadata": {},
138+
"outputs": [],
139+
"source": [
140+
"iterator=iter(my_list)"
141+
]
142+
},
143+
{
144+
"cell_type": "code",
145+
"execution_count": 25,
146+
"metadata": {},
147+
"outputs": [
148+
{
149+
"name": "stdout",
150+
"output_type": "stream",
151+
"text": [
152+
"There are no elements in the iterator\n"
153+
]
154+
}
155+
],
156+
"source": [
157+
"\n",
158+
"\n",
159+
"try:\n",
160+
" print(next(iterator))\n",
161+
"except StopIteration:\n",
162+
" print(\"There are no elements in the iterator\")"
163+
]
164+
},
165+
{
166+
"cell_type": "code",
167+
"execution_count": 26,
168+
"metadata": {},
169+
"outputs": [
170+
{
171+
"name": "stdout",
172+
"output_type": "stream",
173+
"text": [
174+
"H\n",
175+
"e\n"
176+
]
177+
}
178+
],
179+
"source": [
180+
"# String iterator\n",
181+
"my_string = \"Hello\"\n",
182+
"string_iterator = iter(my_string)\n",
183+
"\n",
184+
"print(next(string_iterator)) # Output: H\n",
185+
"print(next(string_iterator)) # Output: e"
186+
]
187+
},
188+
{
189+
"cell_type": "code",
190+
"execution_count": null,
191+
"metadata": {},
192+
"outputs": [],
193+
"source": []
194+
},
195+
{
196+
"cell_type": "code",
197+
"execution_count": null,
198+
"metadata": {},
199+
"outputs": [],
200+
"source": []
201+
},
202+
{
203+
"cell_type": "code",
204+
"execution_count": null,
205+
"metadata": {},
206+
"outputs": [],
207+
"source": []
208+
},
209+
{
210+
"cell_type": "code",
211+
"execution_count": null,
212+
"metadata": {},
213+
"outputs": [],
214+
"source": []
215+
},
216+
{
217+
"cell_type": "code",
218+
"execution_count": null,
219+
"metadata": {},
220+
"outputs": [],
221+
"source": []
222+
}
223+
],
224+
"metadata": {
225+
"kernelspec": {
226+
"display_name": "Python 3",
227+
"language": "python",
228+
"name": "python3"
229+
},
230+
"language_info": {
231+
"codemirror_mode": {
232+
"name": "ipython",
233+
"version": 3
234+
},
235+
"file_extension": ".py",
236+
"mimetype": "text/x-python",
237+
"name": "python",
238+
"nbconvert_exporter": "python",
239+
"pygments_lexer": "ipython3",
240+
"version": "3.12.0"
241+
}
242+
},
243+
"nbformat": 4,
244+
"nbformat_minor": 2
245+
}

0 commit comments

Comments
 (0)