40
40
print_box(" You didn't enter Python :(" )
41
41
```
42
42
43
- That's nice, but where do we a box function like that?
43
+ That's nice, but where do we get a ` print_box ` function like that?
44
44
45
45
## First functions
46
46
@@ -186,7 +186,7 @@ Let's think about what the built-in function input does. It takes an
186
186
argument, and returns a value. Maybe a custom function could do that
187
187
also?
188
188
189
- ```
189
+ ``` py
190
190
>> > def print_twice (message ):
191
191
... print (message)
192
192
... print (message)
@@ -201,21 +201,25 @@ This function can be called in two ways:
201
201
This is the recommended way for functions that take only one or two
202
202
arguments. I would do this in my code.
203
203
204
- >>> print_twice("hi")
205
- hi
206
- hi
207
- >>>
204
+ ``` py
205
+ >> > print_twice(" hi" )
206
+ hi
207
+ hi
208
+ >> >
209
+ ```
208
210
209
211
Positional arguments are great for simple things, but if your
210
212
function takes many positional arguments it may be hard to tell
211
213
which argument is which.
212
214
213
215
- Using a ** keyword argument** :
214
216
215
- >>> print_twice(message="hi")
216
- hi
217
- hi
218
- >>>
217
+ ```py
218
+ >> > print_twice(message = " hi" )
219
+ hi
220
+ hi
221
+ >> >
222
+ ```
219
223
220
224
Keyword arguments are great when your function needs to take many
221
225
arguments, because each argument has a name and it' s easy to see
@@ -278,10 +282,10 @@ that doesn't specify what to return, our function will return None.
278
282
>> > def return_none_2 ():
279
283
... return
280
284
...
281
- >> > x = return_none_1()
282
- >> > y = return_none_2()
283
- >> > print (x, y )
284
- None None
285
+ >> > print ( return_none_1() )
286
+ None
287
+ >> > print (return_none_2() )
288
+ None
285
289
>> >
286
290
```
287
291
@@ -299,8 +303,8 @@ def print_box(message, character):
299
303
print (character * len (message))
300
304
```
301
305
302
- Then we could change our existing code to always call print_box with a
303
- star as the second argument:
306
+ Then we could change our existing code to always call ` print_box ` with
307
+ a star as the second argument:
304
308
305
309
``` py
306
310
print_box(" Hello World" , " *" )
@@ -322,34 +326,46 @@ different character in two ways:
322
326
323
327
- Using a positional argument.
324
328
325
- print_box("Hello World!")
329
+ ``` py
330
+ print_box(" Hello World!" )
331
+ ```
326
332
327
333
- Using a keyword argument.
328
334
329
- print_box(message="Hello World!")
335
+ ```py
336
+ print_box(message = " Hello World!" )
337
+ ```
330
338
331
339
Or we can give it a different character in a few different ways if we
332
340
need to:
333
341
334
342
- Using two positional arguments.
335
343
336
- print_box("Enter a word:", "?")
344
+ ```py
345
+ print_box(" Enter a word:" , " ?" )
346
+ ```
337
347
338
348
- Using two keyword arguments.
339
349
340
- print_box(message="Enter a word:", character="?")
341
- print_box(character="?", message="Enter a word:")
350
+ ```py
351
+ print_box(message = " Enter a word:" , character = " ?" )
352
+ print_box(character = " ?" , message = " Enter a word:" )
353
+ ```
342
354
343
355
- Using one positional argument and one keyword argument.
344
356
345
357
I would probably do this. If an argument has a default value, I
346
358
like to use a keyword argument to change it if needed.
347
359
348
- print_box("Enter a word:", character="?")
360
+ ```py
361
+ print_box(" Enter a word:" , character = " ?" )
362
+ ```
349
363
350
364
However, this doesn' t work:
351
365
352
- print_box(character="?", "Enter a word:")
366
+ ```py
367
+ print_box(character = " ?" , " Enter a word:" )
368
+ ```
353
369
354
370
The problem is that we have a keyword argument before a positional
355
371
argument. Python doesn' t allow this. You don' t need to worry about
@@ -359,44 +375,51 @@ need to:
359
375
# # Exercises
360
376
361
377
** There is a lot to learn with functions, and I don' t expect you to
362
- learn everything at once.** However, there's also lots and lots of free
363
- Python exercises about defining functions you can do. Do many of them
364
- and spend a lot of time with them, so you'll get used to defining
378
+ learn everything at once.** However, there' s also lots of free Python
379
+ exercises about defining functions you can do. Do many of them and
380
+ spend a lot of time with them, so you' ll get used to defining
365
381
functions.
366
382
367
383
1 . Python comes with many built- in functions. Some of the simplest ones
368
384
are abs , all and any . They can be used like this:
369
385
370
386
- abs returns the absolute value of its only argument.
371
387
372
- >>> abs(1)
373
- 1
374
- >>> abs(-1)
375
- 1
376
- >>>
388
+ ```py
389
+ >> > abs (1 )
390
+ 1
391
+ >> > abs (- 1 )
392
+ 1
393
+ >> >
394
+ ```
377
395
378
396
- any returns True if any of the elements of a list is true, and
379
397
False otherwise.
380
398
381
- >>> any([True, False, True])
382
- True
383
- >>> any([False, False, False])
384
- False
385
- >>>
399
+ ```py
400
+ >> > any ([True , False , True ])
401
+ True
402
+ >> > any ([False , False , False ])
403
+ False
404
+ >> >
405
+ ```
386
406
387
407
- all returns True if all elements of a list are true, and False
388
408
otherwise.
389
409
390
- >>> all([True, True, True])
391
- True
392
- >>> all([True, False, True])
393
- False
394
- >>>
410
+ ```py
411
+ >> > all ([True , True , True ])
412
+ True
413
+ >> > all ([True , False , True ])
414
+ False
415
+ >> >
416
+ ```
395
417
396
- Define functions my_abs, my_all and my_any that work the same way
397
- without using built-in functions. Then run the program with IDLE,
398
- or with ` py -i file.py ` on Windows or ` python3 -i file.py ` on other
399
- operating systems. Then try the above examples with your functions.
418
+ Define functions `my_abs` , `my_all` and `my_any` that work the same
419
+ way without using the built- in functions. Then run the program with
420
+ IDLE , or with `py - i file .py` on Windows or `python3 - i file .py` on
421
+ other operating systems. Try the above examples with your
422
+ functions.
400
423
401
424
2 . The box printing function doesn' t really print a box, it prints a
402
425
message between two lines.
@@ -415,7 +438,9 @@ functions.
415
438
and all the exercises because you didn' t know how to define
416
439
functions. Read those parts now, and do the exercises.
417
440
418
- 4 . Use a search engine (e.g. Google) to find more exercises about
441
+
442
+
443
+ 5 . Use a search engine (e.g. Google) to find more exercises about
419
444
defining functions.
420
445
421
446
Answers for the first and second exercise are [here](answers.md).
0 commit comments