您的位置:首页 > 编程语言 > Python开发

python学习足迹(2)

2004-08-09 13:28 525 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: