Think Python C2
Revision as of 12:33, 5 January 2009 by Aymeric Mansoux (talk | contribs) (→Exercise 2.4: solution update + 2.4.2)
Chapter 2: Variables, expressions and statements
Exercise 2.1
Numbers starting with a 0 are interpreted as being "octal" (or base-8). So 010 represents 8 (1 eights and 0 ones), and 011 is nine (1 eights and 1 ones). It can be sometimes be convenient to write computer-specific numbers in octal notation (such as UNIX file permissions), this is because 8 better fits the way computer memory is organized than normal decimal (base-10) notation.
Exercise 2.3
>>> width/2 8 >>> type(width/2) <type 'int'>
>>> width/2.0 8.5 >>> type(width/2.0) <type 'float'>
>>> 1+2*5 11 >>> type(1+2*5) <type 'int'>
>>> delimiter*5 '.....' >>> type(delimiter*5) <type 'str'>
Exercise 2.4
#!/usr/bin/python # 1. Sphere r = 5 pi = 3.14159265 vol = (4.0/3.0)*pi*(r**3) print vol # 2. Books book = 24.95*0.4 order = 60 shipping = (order - 1)*0.75+3 wholesale = book*order+shipping print wholesale