Skip to content

Commit 2c5649a

Browse files
committed
why did github break this :(
1 parent d83ce12 commit 2c5649a

22 files changed

+371
-371
lines changed

advanced/functions.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Now we know [how to define functions](../basics/defining-functions.md).
44
Functions can take arguments, and they will end up with local variables
55
that have the same name. Like this:
66

7-
```py
7+
```python
88
def print_box(message, border='*'):
99
print(border * (len(message) + 4))
1010
print(border, message, border)
@@ -21,7 +21,7 @@ functions and how they are useful.
2121
Function can take multiple arguments, but they can only return one
2222
value. But sometimes it makes sense to return multiple values as well:
2323

24-
```py
24+
```python
2525
def login():
2626
username = input("Username: ")
2727
password = input("Password: ")
@@ -31,7 +31,7 @@ def login():
3131
The best solution is to return a tuple of values, and just unpack that
3232
wherever the function is called:
3333

34-
```py
34+
```python
3535
def login():
3636
...
3737
return username, password
@@ -51,15 +51,15 @@ class](../basics/classes.md) instead and store the values like
5151

5252
Sometimes you might see code like this:
5353

54-
```py
54+
```python
5555
def thing(*args, **kwargs):
5656
...
5757
```
5858

5959
Functions like this are actually quite easy to understand. Let's make a
6060
function that takes `*args` and prints it.
6161

62-
```py
62+
```python
6363
>>> def thing(*args):
6464
... print("now args is", args)
6565
...
@@ -79,7 +79,7 @@ variable name we wanted instead of `args`.
7979

8080
Our function with nothing but `*args` takes no keyword arguments:
8181

82-
```py
82+
```python
8383
>>> thing(a=1)
8484
Traceback (most recent call last):
8585
File "<stdin>", line 1, in <module>
@@ -92,7 +92,7 @@ them to a function by adding a `*`. Actually it doesn't need to be a
9292
list or a tuple, anything [iterable](../basics/loops.md#summary) will
9393
work.
9494

95-
```py
95+
```python
9696
>>> stuff = ['hello', 'world', 'test']
9797
>>> print(*stuff)
9898
hello world test
@@ -104,7 +104,7 @@ hello world test
104104
`**kwargs` is the same thing as `*args`, but with keyword arguments
105105
instead of positional arguments.
106106

107-
```py
107+
```python
108108
>>> def thing(**kwargs):
109109
... print('now kwargs is', kwargs)
110110
...
@@ -130,7 +130,7 @@ Hello World!
130130
Sometimes it's handy to capture all arguments our function takes. We can
131131
combine `*args` and `**kwargs` easily:
132132

133-
```py
133+
```python
134134
>>> def thing(*args, **kwargs):
135135
... print("now args is", args, "and kwargs is", kwargs)
136136
...
@@ -142,7 +142,7 @@ now args is (1, 2) and kwargs is {'b': 4, 'a': 3}
142142
This is often used for calling a function from another "fake function"
143143
that represents it. We'll find uses for this later.
144144

145-
```py
145+
```python
146146
>>> def fake_print(*args, **kwargs):
147147
... print(*args, **kwargs)
148148
...
@@ -163,7 +163,7 @@ moving if it exists already or a `backup` argument that makes it do a
163163
backup of the file just in case the moving fails. So our function would
164164
look like this:
165165

166-
```py
166+
```python
167167
def move(source, destination, overwrite=False, backup=False):
168168
if overwrite:
169169
print("deleting", destination)
@@ -174,7 +174,7 @@ def move(source, destination, overwrite=False, backup=False):
174174

175175
Then we can move files like this:
176176

177-
```py
177+
```python
178178
>>> move('file1.txt', 'file2.txt')
179179
moving file1.txt to file2.txt
180180
>>> move('file1.txt', 'file2.txt', overwrite=True)
@@ -186,7 +186,7 @@ moving file1.txt to file2.txt
186186
This works just fine, but if we accidentally give the function three
187187
filenames, bad things will happen:
188188

189-
```py
189+
```python
190190
>>> move('file1.txt', 'file2.txt', 'file3.txt')
191191
deleting file2.txt
192192
moving file1.txt to file2.txt
@@ -203,7 +203,7 @@ True](../basics/what-is-true.md) and deleted the file. That's not nice.
203203
The solution is to change our move function so that `overwrite` and
204204
`backup` are keyword-only:
205205

206-
```py
206+
```python
207207
def move(source, destination, *, overwrite=False, backup=False):
208208
...
209209
```
@@ -214,7 +214,7 @@ simple: now it's impossible to overwrite by doing `move('file1.txt',
214214
'file2.txt', True)` and the overwrite must be always given like
215215
`overwrite=True`.
216216

217-
```py
217+
```python
218218
>>> move('file1.txt', 'file2.txt')
219219
moving file1.txt to file2.txt
220220
>>> move('file1.txt', 'file2.txt', True)

advanced/iters.md

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
So far we have used for loops with many different kinds of things.
44

5-
```py
5+
```python
66
>>> for name in ['theelous3', 'RubyPinch', 'go|dfish']:
77
... print(name)
88
...
@@ -24,7 +24,7 @@ over the list `['a', 'b', 'c']`. If we can iterate over something, then
2424
that something is **iterable**. For example, strings and lists are
2525
iterable, but integers and floats are not.
2626

27-
```py
27+
```python
2828
>>> for thing in 123:
2929
... print(thing)
3030
...
@@ -38,7 +38,7 @@ TypeError: 'int' object is not iterable
3838

3939
Lists and strings don't change when we iterate over them.
4040

41-
```py
41+
```python
4242
>>> word = 'hi'
4343
>>> for character in word:
4444
... print(character)
@@ -54,7 +54,7 @@ We can also iterate over [files](../basics/files.md), but they remember
5454
their position and we get the content once only if we iterate over them
5555
twice.
5656

57-
```py
57+
```python
5858
>>> with open('test.txt', 'w') as f:
5959
... print("one", file=f)
6060
... print("two", file=f)
@@ -77,7 +77,7 @@ twice.
7777
We have also used [enumerate](../basics/trey-hunner-zip-and-enumerate.md)
7878
before, and it actually remembers its position also:
7979

80-
```py
80+
```python
8181
>>> e = enumerate('hello')
8282
>>> for pair in e:
8383
... print(pair)
@@ -109,7 +109,7 @@ Iterators have a magic method called `__next__`, and there's a built-in
109109
function called `next()` for calling that. Calling `next()` on an
110110
iterator gets the next value and moves it forward. Like this:
111111

112-
```py
112+
```python
113113
>>> e = enumerate('abc')
114114
>>> e.__next__()
115115
(0, 'a')
@@ -122,7 +122,7 @@ iterator gets the next value and moves it forward. Like this:
122122

123123
There's also a built-in `next()` function that does the same thing:
124124

125-
```py
125+
```python
126126
>>> e = enumerate('abc')
127127
>>> next(e)
128128
(0, 'a')
@@ -137,7 +137,7 @@ Here `e` remembers its position, and every time we call `next(e)` it
137137
gives us the next element and moves forward. When it has no more values
138138
to give us, calling `next(e)` raises a StopIteration:
139139

140-
```py
140+
```python
141141
>>> next(e)
142142
Traceback (most recent call last):
143143
File "<stdin>", line 1, in <module>
@@ -150,14 +150,14 @@ and it's best to just try to get a value from it and catch
150150
StopIteration. That's actually what for looping over an iterator does.
151151
For example, this code...
152152

153-
```py
153+
```python
154154
for pair in enumerate('hello'):
155155
print(pair)
156156
```
157157

158158
...does roughly the same thing as this code:
159159

160-
```py
160+
```python
161161
e = enumerate('hello')
162162
while True:
163163
try:
@@ -178,7 +178,7 @@ Now we know what iterating over an iterator does. But how about
178178
iterating over a list or a string? They are not iterators, so we can't
179179
call `next()` on them:
180180

181-
```py
181+
```python
182182
>>> next('abc')
183183
Traceback (most recent call last):
184184
File "<stdin>", line 1, in <module>
@@ -189,7 +189,7 @@ TypeError: 'str' object is not an iterator
189189
There's a built-in function called `iter()` that converts anything
190190
iterable to an iterator.
191191

192-
```py
192+
```python
193193
>>> i = iter('abc')
194194
>>> i
195195
<str_iterator object at 0x7f987b860160>
@@ -208,7 +208,7 @@ StopIteration
208208

209209
Calling `iter()` on anything non-iterable gives us an error.
210210

211-
```py
211+
```python
212212
>>> iter(123)
213213
Traceback (most recent call last):
214214
File "<stdin>", line 1, in <module>
@@ -219,7 +219,7 @@ TypeError: 'int' object is not iterable
219219
If we try to convert an iterator to an iterator using `iter()` we just
220220
get back the same iterator.
221221

222-
```py
222+
```python
223223
>>> e = enumerate('abc')
224224
>>> iter(e) is e
225225
True
@@ -228,14 +228,14 @@ True
228228

229229
So code like this...
230230

231-
```py
231+
```python
232232
for thing in stuff:
233233
print(thing)
234234
```
235235

236236
...works roughly like this:
237237

238-
```py
238+
```python
239239
iterator = iter(stuff)
240240
while True:
241241
try:
@@ -254,7 +254,7 @@ much simpler way to implement iterators. Let's make a function that
254254
creates an iterator that behaves like `iter([1, 2, 3])` using the
255255
`yield` keyword:
256256

257-
```py
257+
```python
258258
>>> def thingy():
259259
... yield 1
260260
... yield 2
@@ -266,7 +266,7 @@ creates an iterator that behaves like `iter([1, 2, 3])` using the
266266
We can only `yield` inside a function, yielding elsewhere raises an
267267
error.
268268

269-
```py
269+
```python
270270
>>> yield 'hi'
271271
File "<stdin>", line 1
272272
SyntaxError: 'yield' outside function
@@ -275,7 +275,7 @@ SyntaxError: 'yield' outside function
275275

276276
Let's try out our thingy function and see how it works.
277277

278-
```py
278+
```python
279279
>>> thingy()
280280
<generator object thingy at 0xb723d9b4>
281281
>>>
@@ -290,7 +290,7 @@ iterators** with some more features that we don't need to care about.
290290

291291
The generator we got works just like other iterators:
292292

293-
```py
293+
```python
294294
>>> t = thingy()
295295
>>> t
296296
<generator object thingy at 0xb72300f4>
@@ -318,7 +318,7 @@ the yields, when do they run? How does Python know when to run what?
318318

319319
Let's find out.
320320

321-
```py
321+
```python
322322
>>> def printygen():
323323
... print("starting")
324324
... yield 1
@@ -336,7 +336,7 @@ That's weird! We called it, but it didn't print "starting"!
336336

337337
Let's see what happens if we call `next()` on it.
338338

339-
```py
339+
```python
340340
>>> got = next(p)
341341
starting
342342
>>> got
@@ -354,7 +354,7 @@ still there just like they were when we left.
354354
A similar thing happens here. Our function is running, but it's just
355355
stuck at the yield and waiting for us to call `next()` on it again.
356356

357-
```py
357+
```python
358358
>>> next(p)
359359
between 1 and 2
360360
2
@@ -382,7 +382,7 @@ for loops work.
382382
that making a list of them would be too slow or the list wouldn't fit in
383383
the computer's memory. So instead of this...
384384

385-
```py
385+
```python
386386
def get_things():
387387
result = []
388388
# code that appends things to result
@@ -391,22 +391,22 @@ def get_things():
391391

392392
...we can do this:
393393

394-
```py
394+
```python
395395
def get_things():
396396
# code that yields stuff
397397
```
398398

399399
Both of these functions can be used like this:
400400

401-
```py
401+
```python
402402
for thing in get_things():
403403
# do something with thing
404404
```
405405

406406
It's actually possible to create an iterator that yields an infinite
407407
number of things:
408408

409-
```py
409+
```python
410410
>>> def count():
411411
... current = 1
412412
... while True:

0 commit comments

Comments
 (0)