1,Python中的資料類型。
NoneType, TypeType(自訂類型), IntType, LongType, FloatType, ComplexType(複數), StringType, UnicodeType,TupleType, ListType, DictType, FunctionType
LongType在python中是沒有長度限制的,這個也是script的優點。
2,filter(), map(), reduce()
filter(function, list) return list. 篩選規律是 function = true Example:
>>>def f(x): return x %2 != 0
...
filter(f, range(2,10) ), 結果是3,5,7,9
map(function, list), return list, 規律是對list進行運算。
>>> def f(x): return x*x
...
>>> map(f,range(1,10))
[1, 4, 9, 16, 25, 36, 49, 64, 81]
reduce類似進行累加,
>>> def add(x,y): return x+y
...
>>> reduce(add,range(1,11))
55
3,用del來刪除list中的元素:
>>> a=[0,1,2,3,4]
>>> del a[0]
>>> a
[1, 2, 3, 4]
>>> del a[1:3]
>>> a
[1, 4]
4,if ... elif ...else 結構。不是elseif啊。
5,raw_input用來得到使用者的輸入,象Console.ReadLine()
>>> a=raw_input("Your name: ")
Your name: hacker.net
>>> a
'hacker.net'
6, while 迴圈:
while <條件>:
語句(注意縮排)
7, for 迴圈:
for var in <list or string>:
語句(縮排)
>>> for str in "hello,world":
... print str
>>> l=['a','b','c']
>>> for s in l:
... print s,
...
a b c
8, try, except:
try:
語句
except:
語句
9,range函數:返回一個list
range(10) = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
range(5,10) = [5, 6, 7, 8, 9]
range(1,10,2) =[1, 3, 5, 7, 9]
range(10,1,-1) = [10, 9, 8, 7, 6, 5, 4, 3, 2]
經常用的遍曆方法,構造一個range
>>> a=['a','b','c']
>>> for i in range(len(a)):
... print a[i],
...
a b c