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.

if (2 ** X) in L:print (2 ** X), 'was found at', L.index(2 ** X)else:print X, 'not found'X = 5L = map(lambda x: 2**x, range(7))print Lif (2 ** X) in L:print (2 ** X), 'was found at', L.index(2 ** X)else:print X, 'not found'Part IV, FunctionsSee “Part IV Exercises” in Chapter 17 for the exercises.1. The basics. There’s not much to this one, but notice that using print (and henceyour function) is technically a polymorphic operation, which does the right thingfor each type of object:% python>>> def func(x): print x...>>> func("spam")spam>>> func(42)42>>> func([1, 2, 3])[1, 2, 3]>>> func({'food': 'spam'}){'food': 'spam'}2. Arguments. Here’s a sample solution. Remember that you have to use print tosee results in the test calls because a file isn’t the same as code typed interactively;<strong>Python</strong> doesn’t normally echo the results of expression statements in files:def adder(x, y):return x + yprint adder(2, 3)print adder('spam', 'eggs')print adder(['a', 'b'], ['c', 'd'])% python mod.py5spameggs['a', 'b', 'c', 'd']656 | Appendix B: Solutions to End-of-Part Exercises

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

Saved successfully!

Ooh no, something went wrong!