Page 91 -
P. 91

sharing your code






           Q:  Don’t I need to import the BIFs in   Q:  I get how range() works, but surely   Q:  So BIFs are actually good for me?

           order to use them in my program?  I could just as easily use a while loop to
                                            do the same thing?              A: BIFs exist to make your programming
           A: No. For all intents and purposes, the                         experience as straightforward as possible
           BIFs are specifically imported into every   A: Yes, you can, but it’s not as elegant   by providing a collection of functions that
           Python program as well as IDLE.  as using range(). Seriously, though,   provide common solutions to common
           Q:   So the BIFs must belong to the   you to write more code, but it also makes it   problems. Since they are included with
                                            the while equivalent not only requires
                                                                             Python, you are pretty much assured that
          __main__ namespace, right?        your responsibility to worry about loop state,   they have been tested to destruction and
                                            whereas range() worries about this for   do “exactly what it says on the tin.” You can
           A: No. They are automatically imported   you. As a general rule, Python programmers   depend on the BIFs. Using them gives your
                                                                             program a leg up and makes you look good.
           into the __main__ namespace, but the   look for ways to reduce the amount of code   So, yes, the BIFs are good for you!
           BIFs have their very own namespace called   they need to write and worry about, which
           (wait for it) __builtins__.      leads to better code robustness, fewer errors,
                                            and a good night’s sleep.




                              Now that you know a bit about the range() BIF, amend your function to use range() to
                              indent any nested lists a specific number of tab-stops.
                              Hint: To display a TAB character on screen using the print() BIF yet avoid taking a new-line
                             (which is print()’s default behavior), use this Python code: print("\t", end='').


            """This is the "nester.py" module and it provides one function called print_lol()
                which prints lists that may or may not include nested lists."""

                                                                   Include the name of the extra argument.
             def print_lol(the_list,                      ):
                 """This function takes a positional argument called "the_list", which
                     is any Python list (of - possibly - nested lists). Each data item in the
                     provided list is (recursively) printed to the screen on it's own line."""

                 for each_item in the_list:                                             Don’t forget to edit
                     if isinstance(each_item, list):                                    the comment.
                         print_lol(each_item)
                     else:                                                  Add code here to take the required
                                                                            number of tab-stops.


                         print(each_item)


                                                                                       you are here 4    55
   86   87   88   89   90   91   92   93   94   95   96