Think Python C2

From XPUB & Lens-Based wiki
Revision as of 11:05, 15 October 2008 by Michael Murtaugh (talk | contribs) (New page: = 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),...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

(4*pi/3)*(5**3)