Oct 2, 2008

Learning python 读书笔记(1--5章)

PART ONE: Getting Started
Chapter 1: A Python Q&A Session
Chapter 2: How python runs programs
1. Source(x.py) --> Byte Code(x.pyc) --> runtime(PVM)
2. CPython, Jython and Python.net
Psyco: Just-in-Time Compiler
3. Frozen Binaries: program + PVM = binary executable program.
Py2exe(for windows), Installer(Linux & Unix), freeze(original)
Chapter3: How you run programs
1. Unix executable scripts: start with #!/usr/local/bin/python
2. raw_input trick: pause the script
3. import and reload
imports are too expensive an operation to repeat, so when we use reload() instead.
4. Attributes: import xx & from xx import yy. yy are the attributes in the python file xx.
5. IDE: IDLE, Komodo, PythonWorks, PythonWin, Visual Python(plugin for VS).
6. text editor: see http://www.python.org/editors

PATR TWO: Types and Operations
Chapter4: Numbers
1. programs  modules  statements  expressions.
2. Numbers: Normal integers(C longs); Long integers(unlimited size); Floating-point(C doubles);Octal and hex literals; Complex number literals.
3. NumPy(Numeric Python).
4. When use a variable that has never been assigned, Pythons report an error rather than a default value.
5. Numeric representation:
>>>b/a #Auto echo output: more digits
>>>print b/a #print rounds off digits.
>>>repr(num) #Used by echos: as cod form
>>>str(num) #Used by print: user-friendly form
6. Division: Classic, Floor and True
x/y: Classic division
x//y: Floor devision
>>>from __future__import division
7. >>>import math
>>>math.pi, math.e
8. >>>int(‘0100’), int(‘0100’, 8), int(‘0x40’, 16) #100, 64, 64
>>>oct(64), hex(64) #’0100’, ‘0x40’
>>>eval(‘100’), eval(‘0100’), eval(‘0x40’) #100, 64, 64
9. >>>a=3 # a: Names (reference) 3: Object
10. >>>L1 = [2, 3, 4]
>>>L2 = L1
>>>L1 = 24 #L2 is not changed
>>>L1[0] = 24 #L2 = [24, 3, 4]
Chapter 5: Strings
1. >>> s = ‘a\nb\tc’
>>> s # output is the original sequence
>>> print s #output is translated sequence
>>>len(s) #5
2. If python doesn’t recognize the character after ‘\’, it simple keeps the backslash in the resulting string.
3. Raw stirng: r’xxxx’
4. u‘xxx’: Unicode string
5. >>> abc = 'abc'
>>> for i in abc:
print i
a
b
c
>>> for i in abc:
print i,
a b c
6. Index and slicing:
>>> num = '123456'
>>> num[1:3], num[5:], num[:-4]
('23', '6', '12')
>>> num[::2], num[::-1], num[0:-3:2]
('135', '654321', '13')
7. String conversion tools:
>>>int(“42”), str(42) #Convert from/to string
>>>string.atoi(“42”), ‘42’ #older techniques
8. You cannot change a string in place(by assigning an index).
9. String formatting
>>> "%s %d %s" %('I', 4, 'you')
'I 4 you'
%s: string
%r: s, but uses repr()
%c: character
%d: decimal (integer)
%i: integer
%u: unsigned integer
%o, %x, %X: Octal, Hex, Hex uppercase
%e, %E: floating-point exponent
%f: float point
%g, %G: floating-point e or f
%%: %
%[(names)][flag][width][.precision]code
>>> x = 1234
>>> res = "integers:...%d...%+d...%-6d...%06d" %(x, x, x, x)
>>> res
'integers:...1234...+1234...1234 ...001234'
10. String methods:
X.method(arguments) == string.method(X, arguments)
list(); join(); capitalize(); center(width)…

0 comments: