Welcome to Computer Programming For Kids (CP4K)

Welcome to the Computer Programming for Kids blog! We are the co-authors of the book "Hello World! Computer Programming for Kids and Other Beginners", released in March 2009. The book is published by Manning Publications. Manning has a web site for the book, which is where you can download the software and other files related to the book. You can purchase it here, at the Manning web site. If you use this link to purchase the print book or e-book from the Manning web site, you can use the discount code aupromo40 at checkout to get a 40% discount. You can also get it through online retailers like Amazon, as well as in many retail locations such as Chapters in Canada and Barnes & Noble in the U.S. This blog is mostly about the book (for now), but anything related to computers and programming, particularly for kids and beginners, is fair game. We will post articles with extra material that didn't make it into the book, and reader feedback and suggestions are always welcome.

Thursday, March 11, 2010

Passing arguments as a list

In the Author Forum on the Manning page for the book, someone asked about passing arguments to a function as a list. Since posting code on that forum doesn't work well, I thought I would post the answer here.

If you are passing a number of arguments to a function, you could pass them as individual arguments, like this:


def printMyLuckyNumbers(num1, num2, num3, num4, num5):
print "Here are your lucky numbers:"
print num1, num2, num3, num4, num5



Then you would call the function like this:



printMyLuckyNumbers(3, 7, 10, 14, 27)



But that has a couple of disadvantages. First, if there are a lot of arguments, it gets messy to type all the variable names. Second, you might not know ahead of time how many arguments you want to pass.

So, another way, that solves both those problems, is to pass a list of arguments instead, like this:



def printMyLuckyNumbers(myNums):
print "Your lucky numbers are:"
for num in myNums:
print num,



Then you would call the function like this:



myLuckyNumbers = [3, 7, 10, 14, 27]
printMyLuckyNumbers(myLuckyNumbers)



In the first example, you are passing 5 separate arguments. In the second example, you are passing a single list. That list happens to have 5 items, in this example. But it would work with any number of items. For example, this would work just fine when calling the second version of the function:



myLuckyNums = [2, 3, 5, 7, 10, 27, 32, 45]
printMyLuckyNumbers(myLuckyNums)



These are very simple examples. You can pass things more complicated than lists. You can pass nested lists (lists of lists, or two-dimensional lists). You can pass objects, which can contain any data structure you care to define. You can pass lists of objects or objects containing lists (or dictionaries, or any other Python data type).

10 comments:

  1. Ok, I call follow this and understand it for one argument in the function! I am interested in how I would do a whole address for example my address, my neighbors, my aunts, etc. When I print them out I would like to see them in typical address formation.
    Thanks
    Gary

    ReplyDelete
  2. That would look something like this:


    def print_address(addr):
    print addr[0]
    print addr[1], addr[2]
    print addr[3] + ', ' + addr[4], addr[5]

    Then you would call it like this:

    address1 = ["Fred Smith", "Apt 123", "98 Maple Lane", "Atlanta", "Georgia", "12345"]

    print_address(address1)


    Hope this helps.

    ReplyDelete
  3. We just got your book and the second program will not work right. It only allows one guess. We checked, double checked and triple checked the typing etc. What could be wrong?

    ReplyDelete
  4. If you send an e-mail to cp4khelp@yahoo.com, and attach the code you are trying to run, we'll have a look and see if we can find the problem.

    Warren Sande

    ReplyDelete
  5. hi to the Sande's
    first. love the book it is just the book i have been looking for so.. Cheers

    just a quick question. have you thought of or in the process of a follow up book to take the material in hello world to the next stage. the next steps in learning programming in python in keeping with the clear explanations and examples which was done so well in hello world

    if not i wish you and your son would consider it as i believe it would be a great benefit for people like myself learning python but need that extra bit of help.


    thanks for you time
    jh

    ReplyDelete
  6. Just wondering, are you guys going to write anymore technical manuals for kids?

    I'd love to see an up-to-date book on some aspect of Web Design that is Kid-Friendly.

    Maybe you could release a series of books on Computer Technology aimed at children. Just a thought...

    ReplyDelete
  7. We don't have any plans at this time to write any other books. This one had a very personal inspiration driving it, and I don't think that would be the case the next time around (if there was a next time).

    That being said, you never know... anything is possible. I don't think we would seek it out, but if someone (e.g. a publisher) approached us with an idea that was appealing, we might consider it.

    ReplyDelete
  8. I had a copy of the E-book but my PC crashed and I lost the file, is it possible for me to get another copy or will I have to buy it?

    ReplyDelete
  9. If you send an e-mail to support@manning.com, they can probably help you.

    ReplyDelete
  10. How about a function taking as many arguments as you want not collected in a list, and collecting those arguments as a list?

    >>> def getAllArgumentsAsList(*arguments):
    return arguments

    >>> getAllArgumentsAsList(432, 4325, 549, 283, 28)
    (432, 4325, 549, 283, 28)
    >>>

    Oh, I didn't get a list, I got a tuple, but for some reason I don't know, tuples are used more often than lists.

    Or collecting the arguments as a list, and then sending it all as individual arguments to the function?

    >>> myLuckyNumbers = [3, 7, 10, 14, 27]
    >>> printMyLuckyNumbers(*myLuckyNumbers)
    Here are your lucky numbers:
    3 7 10 14 27
    >>> apply(printMyLuckyNumbers, myLuckyNumbers) # It is more recommended to use printMyLuckyNumbers(*myLuckyNumbers) than apply(printMyLuckyNumbers, myLuckyNumbers) because apply() will be gone in Python 3.0. If you really want the signature of apply(), it is "def apply(object, args=[], kwargs={}): return object(*args, **kwargs)".
    Here are your lucky numbers:
    3 7 10 14 27
    >>>

    ReplyDelete

Note: Only a member of this blog may post a comment.