@@ -5,33 +5,33 @@ So far we have used for loops with many different kinds of things.
55``` py
66>> > for name in [' theelous3' , ' RubyPinch' , ' go|dfish' ]:
77... print (name)
8- ...
8+ ...
99theelous3
1010RubyPinch
1111go| dfish
1212>> > for letter in ' abc' :
1313... print (letter)
14- ...
14+ ...
1515a
1616b
1717c
18- >> >
18+ >> >
1919```
2020
21- For looping over something is one way to ** iterate** over it. Some other
22- things also iterate, for example, ` ' '.join(['a', 'b', 'c']) ` iterates
23- over the list ` ['a', 'b', 'c'] ` . If we can for loop over something, then
24- that something is ** iterable** . For example, strings and lists are
21+ For looping over something is one way to ** iterate** over it. Some other
22+ things also iterate, for example, ` ' '.join(['a', 'b', 'c']) ` iterates
23+ over the list ` ['a', 'b', 'c'] ` . If we can for loop over something, then
24+ that something is ** iterable** . For example, strings and lists are
2525iterable, but integers and floats are not.
2626
2727``` py
2828>> > for thing in 123 :
2929... print (thing)
30- ...
30+ ...
3131Traceback (most recent call last):
3232 File " <stdin>" , line 1 , in < module>
3333TypeError : ' int' object is not iterable
34- >> >
34+ >> >
3535```
3636
3737## Iterators
@@ -42,36 +42,36 @@ Lists and strings don't change when we iterate over them.
4242>> > word = ' hi'
4343>> > for character in word:
4444... print (character)
45- ...
45+ ...
4646h
4747i
4848>> > word
4949' hello'
50- >> >
50+ >> >
5151```
5252
53- We can also iterate over [ files] ( ../basics/files.md ) , but they change
54- when we do that. They remember their position, so if we iterate over
53+ We can also iterate over [ files] ( ../basics/files.md ) , but they change
54+ when we do that. They remember their position, so if we iterate over
5555them twice we get the content once only.
5656
5757``` py
5858>> > with open (' test.txt' , ' w' ) as f:
5959... print (" one" , file = f)
6060... print (" two" , file = f)
61- ...
61+ ...
6262>> > a = []
6363>> > b = []
6464>> > with open (' test.txt' , ' r' ) as f:
6565... for line in f:
6666... a.append(line)
6767... for line in f:
6868... b.append(line)
69- ...
69+ ...
7070>> > a
7171[' one\n ' , ' two\n ' ]
7272>> > b
7373[]
74- >> >
74+ >> >
7575```
7676
7777We have also used [ enumerate] ( ../basics/trey-hunner-zip-and-enumerate.md )
@@ -81,21 +81,21 @@ before, and it actually remembers its position also:
8181>> > e = enumerate (' hello' )
8282>> > for pair in e:
8383... print (pair)
84- ...
84+ ...
8585(0 , ' h' )
8686(1 , ' e' )
8787(2 , ' l' )
8888(3 , ' l' )
8989(4 , ' o' )
9090>> > for pair in e:
9191... print (pair)
92- ...
93- >> >
92+ ...
93+ >> >
9494```
9595
96- Iterators are ** iterables that remember their position** . For example,
97- ` open('test.txt', 'r') ` and ` enumerate('hello') ` are iterators.
98- Iterators can only be used once, so we need to create a new iterator if
96+ Iterators are ** iterables that remember their position** . For example,
97+ ` open('test.txt', 'r') ` and ` enumerate('hello') ` are iterators.
98+ Iterators can only be used once, so we need to create a new iterator if
9999we want to do another for loop.
100100
101101Here's a picture that hopefully explains this better:
@@ -104,8 +104,8 @@ Here's a picture that hopefully explains this better:
104104
105105## Iterating manually
106106
107- Iterators have a magic method called ` __next__ ` , and there's a built-in
108- function called ` next() ` for calling that. Calling ` next() ` on an
107+ Iterators have a magic method called ` __next__ ` , and there's a built-in
108+ function called ` next() ` for calling that. Calling ` next() ` on an
109109iterator gets the next value and moves it forward. Like this:
110110
111111``` py
@@ -116,7 +116,7 @@ iterator gets the next value and moves it forward. Like this:
116116(1 , ' b' )
117117>> > e.__next__ ()
118118(2 , ' c' )
119- >> >
119+ >> >
120120```
121121
122122There's also a built-in ` next() ` function that does the same thing:
@@ -129,26 +129,26 @@ There's also a built-in `next()` function that does the same thing:
129129(1 , ' b' )
130130>> > next (e)
131131(2 , ' c' )
132- >> >
132+ >> >
133133```
134134
135- Here ` e ` remembers its position, and every time we call ` next(e) ` it
136- gives us the next element and moves forward. When it has no more values
135+ Here ` e ` remembers its position, and every time we call ` next(e) ` it
136+ gives us the next element and moves forward. When it has no more values
137137to give us, calling ` next(e) ` raises a StopIteration:
138138
139139``` py
140140>> > next (e)
141141Traceback (most recent call last):
142142 File " <stdin>" , line 1 , in < module>
143143StopIteration
144- >> >
144+ >> >
145145```
146146
147- There is usually not a good way to check if the iterator is at the end,
148- and it's best to just try to get an value from it and catch
147+ There is usually not a good way to check if the iterator is at the end,
148+ and it's best to just try to get an value from it and catch
149149StopIteration.
150150
151- This is actually what for looping over an iterator does. For example,
151+ This is actually what for looping over an iterator does. For example,
152152this code...
153153
154154``` py
@@ -170,24 +170,24 @@ while True:
170170 print (pair)
171171```
172172
173- The for loop version is much simpler to write and I wrote the while loop
173+ The for loop version is much simpler to write and I wrote the while loop
174174version just to explain what the for loop does.
175175
176176## Converting to iterators
177177
178- Now we know what iterating over an iterator does. But how about
179- iterating over a list or a string? They are not iterators, so we can't
178+ Now we know what iterating over an iterator does. But how about
179+ iterating over a list or a string? They are not iterators, so we can't
180180call ` next() ` on them:
181181
182182``` py
183183>> > next (' abc' )
184184Traceback (most recent call last):
185185 File " <stdin>" , line 1 , in < module>
186186TypeError : ' str' object is not an iterator
187- >> >
187+ >> >
188188```
189189
190- There's a built-in function called ` iter() ` that converts anything
190+ There's a built-in function called ` iter() ` that converts anything
191191iterable to an iterator.
192192
193193``` py
@@ -204,7 +204,7 @@ iterable to an iterator.
204204Traceback (most recent call last):
205205 File " <stdin>" , line 1 , in < module>
206206StopIteration
207- >> >
207+ >> >
208208```
209209
210210Calling ` iter() ` on anything non-iterable gives us an error.
@@ -214,17 +214,17 @@ Calling `iter()` on anything non-iterable gives us an error.
214214Traceback (most recent call last):
215215 File " <stdin>" , line 1 , in < module>
216216TypeError : ' int' object is not iterable
217- >> >
217+ >> >
218218```
219219
220- If we try to convert an iterator to an iterator using ` iter() ` we just
220+ If we try to convert an iterator to an iterator using ` iter() ` we just
221221get back the same iterator.
222222
223223``` py
224224>> > e = enumerate (' abc' )
225225>> > iter (e) is e
226226True
227- >> >
227+ >> >
228228```
229229
230230So code like this...
@@ -248,8 +248,8 @@ while True:
248248
249249## Custom iterables
250250
251- Implementing a custom iterator is easy. All we need to do is to define a
252- ` __next__ ` method that gets the next element, and an ` __iter__ ` method
251+ Implementing a custom iterator is easy. All we need to do is to define a
252+ ` __next__ ` method that gets the next element, and an ` __iter__ ` method
253253that returns the iterator itself. For example, here's an iterator that
254254behaves like ` iter([1, 2, 3]) ` :
255255
0 commit comments