12.07.2015 Views

Is Python a

Is Python a

Is Python a

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

B RAINB UILDERPart IV ExercisesIn these exercises, you’re going to start coding more sophisticated programs. Be sureto check the solutions in “Part IV, Functions” in Appendix B, and be sure to startwriting your code in module files. You won’t want to retype these exercises fromscratch if you make a mistake.1. The basics. At the <strong>Python</strong> interactive prompt, write a function that prints its singleargument to the screen and call it interactively, passing a variety of objecttypes: string, integer, list, dictionary. Then, try calling it without passing anyargument. What happens? What happens when you pass two arguments?2. Arguments. Write a function called adder in a <strong>Python</strong> module file. The functionshould accept two arguments, and return the sum (or concatenation) of the two.Then, add code at the bottom of the file to call the adder function with a varietyof object types (two strings, two lists, two floating points), and run this file as ascript from the system command line. Do you have to print the call statementresults to see results on your screen?3. varargs. Generalize the adder function you wrote in the last exercise to computethe sum of an arbitrary number of arguments, and change the calls to pass moreor less than two arguments. What type is the return value sum? (Hints: a slicesuch as S[:0] returns an empty sequence of the same type as S, and the typebuilt-in function can test types; but see the min examples in Chapter 16 for a simplerapproach.) What happens if you pass in arguments of different types? Whatabout passing in dictionaries?4. Keywords. Change the adder function from exercise 2 to accept and sum/concatenate three arguments: def adder(good, bad, ugly). Now, provide defaultvalues for each argument, and experiment with calling the function interactively.Try passing one, two, three, and four arguments. Then, try passingkeyword arguments. Does the call adder(ugly=1, good=2) work? Why? Finally,generalize the new adder to accept and sum/concatenate an arbitrary number ofkeyword arguments. This is similar to what you did in exercise 3, but you’ll needto iterate over a dictionary, not a tuple. (Hint: the dict.keys( ) method returns alist you can step through with a for or while.)5. Write a function called copyDict(dict) that copies its dictionary argument. Itshould return a new dictionary containing all the items in its argument. Use thedictionary keys method to iterate (or, in <strong>Python</strong> 2.2, step over a dictionary’s keyswithout calling keys). Copying sequences is easy (X[:] makes a top-level copy);does this work for dictionaries too?6. Write a function called addDict(dict1, dict2) that computes the union of twodictionaries. It should return a new dictionary containing all the items in both itsarguments (which are assumed to be dictionaries). If the same key appears inPart IV Exercises | 379

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!