|
string vs list
string 也和 list 同为 sequence data type,也可作 slice operator,但 string 为 immutable object,不可修改其内容。
1 a = 'hello world!!'
2 print a[-7:], a[:5]
结果显示 : world!! hello
1 a = 'hello'
2 print a[1]
结果显示 : e
string 也和 list 一样,可以 subscript (index) 其中的 item。python 并不像 C/C++ 般, python 并没有独立的 character type,string 的 subscripted item 为一长度为 1 的 string。
nested list
list 里的 item 可以为另一个 list object,成一个巢状的结构。
1 a = [ 1, 2, 3, ][ 'abc', 'cde', 3, 2], 5]
2 print a[3][1]
结果显示 : cde
1 a = [1, 2, 3]
2 print len(a)
结果显示 : 3
len() 函数传回 sequence type object 的 size。
tuple & multiple assignment
1 a = 'foo'
2 a, b, c = 9, 8, a
3 print a, b, c
结果显示 9 8 foo
行 2 称为 multiple assignment,可以同时指定多个变数的内容。
1 a = 1, 2, 3, 4, 5
2 b = 1, 2, a[:3]
3 print a, b
结果显示 : (1, 2, 3, 4, 5) (1, 2, (1, 2, 3))
行 1 bind name a 为 (1, 2, 3, 4, 5),python 定义为 tuple,同 string 为 immutable sequence data type, 不能改变 tuple object 的内容。a = 1, 2, 3, 4, 5 和 a = (1, 2, 3, 4, 5) 是相同的,称之为 tuple packing, 把 1, 2, 3, 4, 5 这几个 object 包成一个 tuple object。
1 a, b, c = 1, 2, 3
2 (e, f, g) = 5, 6, 7
3 print a, b, c, d, e
结果显示 : 1 2 3 5 6 7
上面的动作称之为 tuple unpacking,将等号右边的 sequence type object 解开成数个 object 和等号左边的 name 进行 bind。左边 tuple 的长度必需和右边的 item 数目相同。
1 a = 'hello'
2 a, b, c, d, e = a
3 print d, e, a, b, c
结果显示 : l o h e l
在 multiple assignment 的右边可以为任何 sequence type。
Dictionary
1 tel = { 'tony': '111234', 988: 'common' }
2 print tel[988], tel['tony']
结果显示 : common 111234
这为 dictionary (assocative array) 的使用范例。可使用任一 immutable type object 为 key, 用以对映(mapping)指定之 object。
1 a = (1, 2)
2 b = (3, 4)
3 c = (1, 2)
4 d = { a: 'aaa', b: 'bbb' }
5 print d[a], d, d[c]
结果显示 : aaa bbb aaa
tuple 也为 immutable object,所以也可作为 key。因为 a 和 c 所 bind 的 object 为相同 value 的 immutable type object,因此得到的结果是相同的。tuple 为 key 时, 其内容不可包含任何 mutable type object。
1 a = { 'foo': 'aaa', 'boo', 999, 'coo', 887}
2 print a.keys()
结果显示 : ['boo', 'foo', 'coo']
keys() 为 dictionary 的 method,传回包含所有 key 的 list object。key 的放置不依其次序。
1 a = { 'aaa': 9999, 'bbb', 8888 }
2 for i in 'aaa', 'bbb', 'ccc':
3 if a.has_key(i):
4 print a,
结果显示 : 9999 8888
has_key() 为 dictionary 的 method function,用以判断 dictionary 是否包含某 key。
1 a = { 1: 'aaa', 2: 'bbb', 3: 'ccc'}
2 del a[2]
3 b = ['aaa', 'bbb', 'ccc']
4 del b[1]
3 print a, b
结果显示 : { 1:'aaa', 3:'ccc'} ['aaa', 'ccc']
del 指含可以打断 object 和 key 之间的 binding,并将 key 从 dictionary 去除。可以将 list 中的 elemnet 去除。
程式流程、函数与模组
Fllow control
1 if a <= b:
2 if c == d:
3 foo()
4 aoo()
5 else:
6 boo()
上面一个 if 的使用范例,在行 1,如果 a 小於等於 b 行 2~4 会被执行,否则行 6 会被执行。 python 并没有像 Pascal 使用 begin ... end 定义 block,也没有像 C 使用 { ... } 定义 block, python 直接使用缩排决定statement 是否属於同一个 block。如 行1-4 因为缩排而成为一个 block, 而行 2-3 也因为缩排而成为一个 block,每一个 block 则成为一个 compound statement。if statement 的语法规式为 :
if : .....
elif : ......
elif : ....
else: .......
如其它语言,elif 和 else 为选择性功能,elif => else if
1 while a >= b:
2 foo()
3 boo()
4 if c != d: break
5 if c >= d: continue
6 coo()
7 else:
8 doo()
9 eoo()
上面是 while loop 的使用范例,当 a >= b 时,行 2-6会不断的一再执行,直到 a >= b 不成立时(false), 则执行行 8,然後继续往下执行 eoo()。但,如果执行行 4 的 break,则会无条件直接跳离 while loop, 不再执行 while loop 的任何内容(包含 else 的部分),直接执行行 9。若 while loop 中,continue 被执行, 则程式会直接跳过後面的指令回到行 1,进行条件判断。
1 for i in 1, 7, 3, 9:
2 print i,
结果显示 : 1 7 3 9
上面为 for loop 的使用法,for in : ....in 後面可为任何 sequence type object。
1 for i in range(3):
2 print i,
3 print range(3)
结果显示 : 0 1 2 [0, 1, 2]
range(x) 会产生一个包含 0 ~ x-1 的 list。
Function
1 def foo(a, b):
2 return a+b
3 print foo(3, 4)
结果显示 : 7
上面为 function 的定义方式,def (, ....): ....return 可回传 function 的执行结果。 当行 3 执行 foo(3, 4),function foo 会被执行,然後传回 3+4 (a+b) 的结果。
1 def foo(a, b=2):
2 return a+b
3 print foo(3), foo(3,4)
结果显示 : 5 7
function 的参数可以定义预设值,设立预设值的参数如果呼叫时不给予,那麽会使用预设值。
1 def foo(a = []):
2 a.append(7)
3 return a
4 print foo(),
5 print foo()
结果显示 : [7] [7, 7]
预设值只会在 function 定义指令第一次被执行时进行计算,因此上例的 a 参数在第一次执行定义指令时, 就将预设值 bind 到一个 list object,之後使用预设值时就会 bind 到同一个 object,因而产生记忆的现象, 使第二次执行 foo() 所得到的结果受第一次执行的影向。
Module
Python 的 source file 为 *.py 这样的形式,如: foo.py boo.py。Python 定义每一个 source file 为一个 Module。
下面两个 module :
--- a.py ----
def foo():
return action+1
action = 9
--- a.py end ---
--- b.py ---
import a
print a.foo(), a.action
--- b.py ---
执行 'python b.py',结果显示 :
10 9
当需要使用到其它 module 时,可以使用 import 指今,将该 module import 到你的 name space 里。 上例 b modlue 就 import a 这个 modlue,当 b 执行 import 指令时,在 b 的 name space 里, a 这个 name 就会 bind 到一个 module object。module 一但 load 进 memory 时, 就以 module object 代表,以供进一部的使用。所有 module 里定义的 name(function,object 或後面会介绍的 class)都会成为 module object 的 attribute。透过使用 module object attribute, 我们可以 access module 内所定义之 name。module attribute 的使用方式如上例, module 的 name 接着 '.' 接着 attribute 的 name。每个 module 都有自己的 name space, 透过 module attribute,我们可以 access 到其它 module 的 name space。
当 module 头一次被 load 进 memory 时,module 会被从头到尾执行一次。 其执行的结果则定义了 module 的 name space,使 name 和 object bind 在一起。 如上例执行 a.py 中的 def foo(): .... 这个指令时,定义一个 function object, 并使之和 foo 这个 name 进行 binding。
--- a.py ----
def foo():
return action+1
action = 9
--- a.py end ---
--- b.py ---
from a import *
print foo(), action
--- b.py ---
上例和前一个例子是相同的,但使用 from import *,这个指令的作用是, 把 module name space 里所有的 name,import 到目前的 name space 里。'*' 是指所有的 name, 如果只是要 import 特定的 name,那麽你可以指定 name 替代 '*',如 from a import foo。 |
|