4040 print_box(" You didn't enter Python :(" )
4141```
4242
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?
4444
4545## First functions
4646
@@ -186,7 +186,7 @@ Let's think about what the built-in function input does. It takes an
186186argument, and returns a value. Maybe a custom function could do that
187187also?
188188
189- ```
189+ ``` py
190190>> > def print_twice (message ):
191191... print (message)
192192... print (message)
@@ -201,21 +201,25 @@ This function can be called in two ways:
201201 This is the recommended way for functions that take only one or two
202202 arguments. I would do this in my code.
203203
204- >>> print_twice("hi")
205- hi
206- hi
207- >>>
204+ ``` py
205+ >> > print_twice(" hi" )
206+ hi
207+ hi
208+ >> >
209+ ```
208210
209211 Positional arguments are great for simple things, but if your
210212 function takes many positional arguments it may be hard to tell
211213 which argument is which.
212214
213215- Using a ** keyword argument** :
214216
215- >>> print_twice(message="hi")
216- hi
217- hi
218- >>>
217+ ```py
218+ >> > print_twice(message = " hi" )
219+ hi
220+ hi
221+ >> >
222+ ```
219223
220224 Keyword arguments are great when your function needs to take many
221225 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.
278282>> > def return_none_2 ():
279283... return
280284...
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
285289>> >
286290```
287291
@@ -299,8 +303,8 @@ def print_box(message, character):
299303 print (character * len (message))
300304```
301305
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:
304308
305309``` py
306310print_box(" Hello World" , " *" )
@@ -322,34 +326,46 @@ different character in two ways:
322326
323327- Using a positional argument.
324328
325- print_box("Hello World!")
329+ ``` py
330+ print_box(" Hello World!" )
331+ ```
326332
327333- Using a keyword argument.
328334
329- print_box(message="Hello World!")
335+ ```py
336+ print_box(message = " Hello World!" )
337+ ```
330338
331339Or we can give it a different character in a few different ways if we
332340need to:
333341
334342- Using two positional arguments.
335343
336- print_box("Enter a word:", "?")
344+ ```py
345+ print_box(" Enter a word:" , " ?" )
346+ ```
337347
338348- Using two keyword arguments.
339349
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+ ```
342354
343355- Using one positional argument and one keyword argument.
344356
345357 I would probably do this. If an argument has a default value, I
346358 like to use a keyword argument to change it if needed.
347359
348- print_box("Enter a word:", character="?")
360+ ```py
361+ print_box(" Enter a word:" , character = " ?" )
362+ ```
349363
350364 However, this doesn' t work:
351365
352- print_box(character="?", "Enter a word:")
366+ ```py
367+ print_box(character = " ?" , " Enter a word:" )
368+ ```
353369
354370 The problem is that we have a keyword argument before a positional
355371 argument. Python doesn' t allow this. You don' t need to worry about
@@ -359,44 +375,51 @@ need to:
359375# # Exercises
360376
361377** 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
365381functions.
366382
3673831 . Python comes with many built- in functions. Some of the simplest ones
368384 are abs , all and any . They can be used like this:
369385
370386 - abs returns the absolute value of its only argument.
371387
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+ ```
377395
378396 - any returns True if any of the elements of a list is true, and
379397 False otherwise.
380398
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+ ```
386406
387407 - all returns True if all elements of a list are true, and False
388408 otherwise.
389409
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+ ```
395417
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.
400423
4014242 . The box printing function doesn' t really print a box, it prints a
402425 message between two lines.
@@ -415,7 +438,9 @@ functions.
415438 and all the exercises because you didn' t know how to define
416439 functions. Read those parts now, and do the exercises.
417440
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
419444 defining functions.
420445
421446Answers for the first and second exercise are [here](answers.md).
0 commit comments