Skip to content

Commit 614a181

Browse files
committed
add stuff
1 parent f6777d6 commit 614a181

4 files changed

Lines changed: 79 additions & 0 deletions

File tree

todo/dicts-and-tuples.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Dictionaries and tuples
2+
3+
So far we know how to store multiple

todo/genexample.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def thing():
2+
print("starting")
3+
print("blah blah blah")
4+
print("yielding 1")
5+
yield 1
6+
# -------------------- CUT HERE --------------------
7+
print("running")
8+
print("blah blah blah")
9+
print("yielding 2")
10+
yield 2
11+
# -------------------- CUT HERE --------------------
12+
print("done")
13+
14+
15+
t = Thing() # run nothing at all
16+
print("got from next(t):", next(t)) # run the first piece
17+
print("got from next(t):", next(t)) # run the second piece
18+
print("got from next(t):", next(t)) # run the last piece and raise StopIteration

todo/iterators.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Iterables and iterators
2+
3+
We have used for loops in many exercises and other things so far. One of
4+
the simplest things we can for loop over are lists.
5+
6+
```py
7+
>>> for item in ['a', 'b', 'c']:
8+
... print(item)
9+
...
10+
a
11+
b
12+
c
13+
>>>
14+
```
15+
16+
But what exactly is happening behind the scenes?
17+
18+
## Iterables
19+
20+
An **iterable** is anything we can put after a `for item in`. For
21+
example, strings, lists, tuples and dictionaries are all iterable.
22+
Iterating over an iterable is simple:
23+
24+
```py
25+
string = 'hello'
26+
for character in string:
27+
print(character)
28+
```
29+
30+
You might think that under the covers Python does something like this:
31+
32+
```py
33+
string = 'hello'
34+
index = 0
35+
while index < len(string):
36+
print(string[index])
37+
index += 1
38+
```
39+
40+
But actually, th

todo/yield-example.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def thing():
2+
print("starting")
3+
print("blah blah blah")
4+
print("yielding 1")
5+
yield 1
6+
# -------------------- CUT HERE --------------------
7+
print("running")
8+
print("blah blah blah")
9+
print("yielding 2")
10+
yield 2
11+
# -------------------- CUT HERE --------------------
12+
print("done")
13+
14+
15+
t = Thing() # run nothing at all
16+
print("got", next(t)) # run the first piece
17+
print("got", next(t)) # run the second piece
18+
print("got", next(t)) # run the last piece and raise StopIteration

0 commit comments

Comments
 (0)