Python中的元组和集合
1、打开Python开发工具IDLE,新建‘tuple.py’,并写代码如下:t1 = (1,2,'3')print (t1)print (t1[0])t1[0]=8元组是两个小括号括起来

2、F5运行程序,打印出元组所有元素和第一个元素,字斤谯噌最后报错,因为元组的元素不支持赋值操作,只能读取(1, 2, '3'像粜杵泳)1Traceback (most recent call last): File "C:/Python27/tuple", line 4, in <module> t1[0]=8TypeError: 'tuple' object does not support item assignment

3、改写代码,测试Python中的集合set,代码如下:set1 = {1,2,3}print (set1)print (set1[0])集合是{}括起来

4、F5运行程序,打印出集合内容, 通过索引打印会韫蛛泌尾报错,集合不支持索引操作set([1, 2, 3])Trace芟鲠阻缒back (most recent call last): File "C:/Python27/tuple", line 4, in <module> print (set1[0])TypeError: 'set' object does not support indexing

5、集合最常用的操作是去除重复数据,代码如下:list1 = [1,2,6,'a',1,2]set1=set(list1)print (set1)print (list(set1))通过转换成集合,可以给列表去重

6、F5运行程序,打印去重后的列表set(['a', 1, 2, 6])['a', 1, 2, 6]
