12.07.2015 Views

Is Python a

Is Python a

Is Python a

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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Package this code as a reusable function in a module file, and add some calls tothe function at the bottom of your file. While you’re at it, replace the first line’s /operator with //, so it can handle floating-point numbers, too, and is immune tothe true division change planned for the / operator in <strong>Python</strong> 3.0 (described inChapter 5). What can you do about negatives, and the values 0 and 1? Howabout speeding this up? Your outputs should look something like this:13 is prime13.0 is prime15 has factor 515.0 has factor 5.09. List comprehensions. Write code to build a new list containing the square roots ofall the numbers in this list: [2, 4, 9, 16, 25]. Code this as a for loop first, then asa map call, and finally as a list comprehension. Use the sqrt function in the built-inmath module to do the calculation (i.e., import math and say math.sqrt(x)). Of thethree, which approach do you like best?Part IV Exercises | 381

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

Saved successfully!

Ooh no, something went wrong!