Page 65 -
P. 65

meet python


           Create a function in Python


          A function in Python is a named suite of  code, which can also take an optional
           list of arguments if required.

          You define a Python function using the def statement, providing a name
           for your function and specifying either an empty or populated argument list     A colon (:) follows the
           within parentheses. The standard form looks something like this:                closing parenthesis
                                                              Argument lists are optional,   and indicates the
                                                                                           start of your
        The keyword “def”                                     but the parentheses are NOT.   functions code suite.
         introduces the name
         of the function.
                            def        (       ):
                                         function name
                                                                    argument(s)



                 The function’s code            function code suite
                  MUST be indented under
                  the def statement.



           What does your function need to do?
          Your function needs to take a list and process each item in the list. If it finds a
           nested list within the first list, the function needs to repeat. It can do this by
           invoking itself on the nested list. In other words, the function needs to recur—
           that is, invoke itself from within the funtion code suite.




                                                Let’s call the function that you’ll create print_lol(). It takes
                                                one argument: a list to display on screen. Grab your pencil and
                                                complete the code below to provide the required functionality:

                             def print_lol(the_list):


                                     for
                                            if



                                            else:





                                                                                       you are here 4    29
   60   61   62   63   64   65   66   67   68   69   70