分别定义字符串,列表,元组,字典,集合,并进行遍历。 总结列表,元组,字典,集合的联系与区别。 字符串 ls='handsomeboy' for i in ls: print(i) 列表: 1 2 3 4 5 6 7 str1 = ['nike','vans',[1996,7,28]] for i in str1: print(i) nike vans [1996, 7, 28] 元组: 复制代码 str2 = ('nike','vans',[1996,7,28]) for i in str2: print(i) nike vans [1996, 7, 28] 复制代码 字典: 复制代码 dic = dict(zip(['w','o','c'],['5','2','1'])) for i in dic.keys(): print(i) for i in dic.values(): print(i) for i in dic.items(): print(i) 复制代码 集合: A = set('abcd') B = set('cdef') s=A&B for i in s: print(i) 总结列表,元组,字典,集合的联系与区别。 联系:都是一组数据集合在一个容器内 区别:字典和集合能存储任意类型对象,且对象都是无序性的并不重复的。而列表和元组则不是。 列表有索引值,从0开始 元组中的元素值是不允许修改的,但可以对元组进行连接组合 字典有键值且必须有键值,列表等则没有。 集合可以有任意数量的元素,它们可以是不同的类型(例如:数字、元组、字符串等)。但是,集合不能有可变元素(例如:列表、集合或字典)。