Python 复习笔记(二)

1. 切片:[元素起占:元素终点:步长],如:
     numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
     numbers[1:10:2]
     输出结果:[2, 4, 6, 8, 10]
     numbers[5:-3]
     输出结果:[6, 7]  # 取第5个元素为起点,倒数第3个元素为终点之间的元素

2. 序列相加时,只能同类型的序列相加,不同类型不能相加,如:
   [1, 2, 3] + [4, 5, 6] # okay
   [1, 2, 3] + 'abc'     # error

3. 序列乘法:
   sequence = [None] * 10 # [None, None, None, None, None, None, None, None, None, None, ]

4. in 运算符,检查一个对象是否为某个序列的成员,返回值:True/False
   permissions = 'rw'
   'w' in permissions

5. 长度、最大值与最小值 (len, max, min)
    numbers = [100, 34, 678]
    len(numbers) # 3
    max(numbers) # 678,如果列表中的类型有字符或字符串,将首字符转换为整型再做比较
    min(numbers) # 34


6. 用字符串创建列表:list
   list('Hello')

posted @ 2011-10-26 10:50  jeff_nie  阅读(149)  评论(0编辑  收藏  举报