序列:包括列表、元祖和字符串等,共同特点如下:
1)都可以通过索引得到每一个元素
2)默认索引值总是从0开始(Python还支持负数索引)
3)可以通过分片的方法得到一个范围内的元素的集合
4)有很多共同的操作符(重复操作符*、拼接操作符+、成员关系操作符in)
>>> a='I love FishC.com'>>> list(a)['I', ' ', 'l', 'o', 'v', 'e', ' ', 'F', 'i', 's', 'h', 'C', '.', 'c', 'o', 'm']>>> max(1,3.14,556,-5)556>>> min(1,3.14,556,-5)-5>>> tuple1=[1,2,3,4,5,6]>>> max(tuple1)6>>> sum(tuple1)21>>> sum(tuple1,100)121>>> sorted(tuple1)[1, 2, 3, 4, 5, 6]>>> reversed(tuple1)>>> list(reversed(tuple1))[6, 5, 4, 3, 2, 1]>>> enumerate(tuple1) >>> list(enumerate(tuple1))[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]
list:len/sum/max/min/sorted/renversed/enumerate/zip
max=tuple1[0]for each in tuple1: if each>max: max=eachreturn max
def min(x): least = x[0] for each in x: if each < least: least = each return leastprint(min('123456789'))
def sum(x): result = 0 for each in x: if (type(each) == int) or (type(each) == float): result += each else: continue return resultprint(sum([1, 2.1, 2.3, 'a', '1', True]))
def sum(x):
result = 0
for each in x:
if (type(each) == int) or (type(each) == float):
result += each
else:
continue
return result
print(sum([1, 2.1, 2.3, 'a', '1', True]))