Skip to content

Commit 60154fd

Browse files
committed
add tray hunner stuff and fix stupid mistakes in answers.md
1 parent 4dd9b2d commit 60154fd

3 files changed

Lines changed: 272 additions & 8 deletions

File tree

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ with other tutorials.
3939
8. [ThinkPython: Lists](lists.md)
4040
9. [Handy stuff with strings](handy-stuff-strings.md)
4141
10. [Loops](loops.md)
42-
11. [Files](files.md)
43-
12. [Defining functions](defining-functions.md)
44-
13. [Modules](modules.md)
42+
11. [Trey Hunner: zip and enumerate](trey-hunner-zip-and-enumerate.md)
43+
12. [Files](files.md)
44+
13. [Defining functions](defining-functions.md)
45+
14. [Modules](modules.md)
4546

4647
Parts of this tutorial that aren't ready to be added to the rest of it
4748
yet:

answers.md

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,77 @@ These are answers for exercises in the chapters. In programming, there's always
195195
print("Wrong username or password.")
196196
```
197197

198+
## Trey Hunner: zip and enumerate
199+
200+
1. Read some lines with `input` and then enumerate it.
201+
202+
```py
203+
print("Enter something, and press Enter without typing anything",
204+
"when you're done.")
205+
206+
lines = []
207+
while True:
208+
line = input('>')
209+
if line == '':
210+
break
211+
lines.append(line)
212+
213+
for index, line in enumerate(lines, start=1):
214+
print("Line", index, "is:", line)
215+
```
216+
217+
2. Let's start by trying out `zip` with strings:
218+
219+
```py
220+
>>> for pair in zip('ABC', 'abc'):
221+
... print(pair)
222+
...
223+
('A', 'a')
224+
('B', 'b')
225+
('C', 'c')
226+
>>>
227+
```
228+
229+
Great, it works just like it works with lists. Now let's create
230+
the letter printing program:
231+
232+
```py
233+
uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
234+
lowercase = 'abcdefghijklmnopqrstuvwxyz'
235+
236+
for upper, lower in zip(uppercase, lowercase):
237+
print(upper, lower)
238+
```
239+
240+
3. This one is a bit more difficult than the other two because we
241+
need to combine `zip` and `enumerate`. I would pass a `zip`
242+
result to `enumerate`, like this:
243+
244+
```py
245+
uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
246+
lowercase = 'abcdefghijklmnopqrstuvwxyz'
247+
248+
for index, letterpair in enumerate(zip(uppercase, lowercase), start=1):
249+
upper, lower = letterpair
250+
print(index, upper, lower)
251+
```
252+
253+
Another alternative is to pass an `enumerate` result to `zip`. This is
254+
a bit more complicated, so I wouldn't do it this way.
255+
256+
```py
257+
uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
258+
lowercase = 'abcdefghijklmnopqrstuvwxyz'
259+
260+
for upper, indexlowerpair in zip(uppercase, enumerate(lowercase, start=1)):
261+
index, lower = indexlowerpair
262+
print(index, upper, lower)
263+
```
264+
198265
## Defining functions
199266

200-
1. Use `-value` to get the negative in the abs function, and for loops
201-
in the other two functions.
267+
1. Use `-value` (it works just like `-1`) to get the negative in
268+
the abs function, and for loops in the other two functions.
202269

203270
```py
204271
def my_abs(value):
@@ -213,7 +280,7 @@ These are answers for exercises in the chapters. In programming, there's always
213280
for item in a_list:
214281
if item:
215282
return True # ends the function
216-
return True
283+
return False
217284

218285
def my_all(a_list):
219286
for item in a_list:
@@ -225,10 +292,10 @@ These are answers for exercises in the chapters. In programming, there's always
225292
2. Like this:
226293

227294
```py
228-
def box(message, character='*'):
295+
def print_box(message, character='*'):
229296
number_of_characters = len(message) + 4
230297
print(character * number_of_characters)
231-
print(message)
298+
print(character, message, character)
232299
print(character * number_of_characters)
233300
```
234301

trey-hunner-zip-and-enumerate.md

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
# Trey Hunner: zip and enumerate
2+
3+
Now we know how [for loops](loops.md#for-loops) work in Python. But
4+
for loops aren't limited to printing each item in a list, they can
5+
do a lot more.
6+
7+
To be able to understand for loop tricks we need to first know
8+
assigning values to multiple variables at once. It works like this:
9+
10+
```py
11+
>>> a, b = 1, 2
12+
>>> a
13+
1
14+
>>> b
15+
2
16+
>>>
17+
```
18+
19+
We can use `()` and `[]` around these values however we want and
20+
everything will still work the same way. `[]` creates a list, and
21+
`()` creates a tuple. We'll talk more about tuples later.
22+
23+
```py
24+
>>> [a, b] = (1, 2)
25+
>>> a
26+
1
27+
>>> b
28+
2
29+
>>>
30+
```
31+
32+
We can also have `[]` or `()` on one side but not on the other
33+
side.
34+
35+
```py
36+
>>> (a, b) = 1, 2
37+
>>> a
38+
1
39+
>>> b
40+
2
41+
>>>
42+
```
43+
44+
What actually happened is that Python created a tuple automatically.
45+
46+
```py
47+
>>> 1, 2
48+
(1, 2)
49+
>>>
50+
```
51+
52+
If we're for looping over a list with pairs of values in it we
53+
could do this:
54+
55+
```py
56+
>>> items = [('a', 1), ('b', 2), ('c', 3)]
57+
>>> for pair in items:
58+
... a, b = pair
59+
... print(a, b)
60+
...
61+
a 1
62+
b 2
63+
c 3
64+
>>>
65+
```
66+
67+
Or we can tell the for loop to unpack it for us.
68+
69+
```py
70+
>>> for a, b in items:
71+
... print(a, b)
72+
...
73+
a 1
74+
b 2
75+
c 3
76+
>>>
77+
```
78+
79+
Now you're ready to read [this looping
80+
tutorial](http://treyhunner.com/2016/04/how-to-loop-with-indexes-in-python/).
81+
Read it now, then read the summary and do the exercises.
82+
83+
## Summary
84+
85+
Assigning multiple values at once:
86+
87+
```py
88+
>>> a, b, c = 1, 2, 3
89+
>>> a
90+
1
91+
>>> b
92+
2
93+
>>> c
94+
3
95+
>>>
96+
```
97+
98+
For looping over a list of pairs:
99+
100+
```py
101+
>>> stuff = [('a', 1), ('b', 2), ('c', 3)]
102+
>>> for a, b in stuff:
103+
... print(a, b)
104+
...
105+
a 1
106+
b 2
107+
c 3
108+
>>>
109+
```
110+
111+
For looping over two lists at once:
112+
113+
```py
114+
>>> colors = ['red', 'green', 'blue']
115+
>>> letters = ['R', 'G', 'B']
116+
>>> for letter, color in zip(letters, colors):
117+
... print(letter, color)
118+
...
119+
R red
120+
G green
121+
B blue
122+
>>>
123+
```
124+
125+
For looping over a list with indexes:
126+
127+
```py
128+
>>> fillernames = ['foo', 'bar', 'biz', 'baz', 'spam', 'eggs']
129+
>>> for index, name in enumerate(fillernames):
130+
... print(index, name)
131+
...
132+
0 foo
133+
1 bar
134+
2 biz
135+
3 baz
136+
4 spam
137+
5 eggs
138+
>>>
139+
```
140+
141+
## Exercises
142+
143+
1. Create a program that works like this. Here I entered everything
144+
after the `>` prompt that the program displayed.
145+
146+
```
147+
Enter something, and press Enter without typing anything when you're done.
148+
>hello there
149+
>this is a test
150+
>it seems to work
151+
>
152+
Line 1 is: hello there
153+
Line 2 is: this is a test
154+
Line 3 is: it seems to work
155+
```
156+
157+
2. Create a program that prints all letters from A to Z and a to z
158+
next to each other:
159+
160+
```
161+
A a
162+
B b
163+
C c
164+
...
165+
X x
166+
Y y
167+
Z z
168+
```
169+
170+
Start your program like this:
171+
172+
```py
173+
uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
174+
lowercase = 'abcdefghijklmnopqrstuvwxyz'
175+
```
176+
177+
**Hint:** how do strings behave with `zip`? Try it out on the
178+
`>>>` prompt and see.
179+
180+
3. Can you also make it print the indexes also?
181+
182+
```
183+
1 A a
184+
2 B b
185+
3 C c
186+
...
187+
24 X x
188+
25 Y y
189+
26 Z z
190+
```
191+
192+
***
193+
194+
You may use this tutorial freely at your own risk. See [LICENSE](LICENSE).
195+
196+
[Back to the list of contents](README.md)

0 commit comments

Comments
 (0)