python学习足迹(2)

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

原文地址:https://www.cnblogs.com/Hacker/p/31359.html