- # 直接使用{}进行创建
- s={10,20,30,40}
- print(s)
-
- # 使用内置函数set()创建
- s=set()
- print(s)
-
- # 创建一个空的{}默认是字典类型
- s={}
- print(s,type(s))
-
- s=set('helloworld')
- print(s)
- s=set([10,20,30])
- print(s)
- s1=set(range(1,10))
- print(s1)
-
- print('max:',max(s1))
- print('min:',min(s1))
- print('len:',len(s1))
- {40, 10, 20, 30}
- set()
- {} <class 'dict'>
- {'h', 'l', 'w', 'r', 'd', 'e', 'o'}
- {10, 20, 30}
- {1, 2, 3, 4, 5, 6, 7, 8, 9}
- max: 9
- min: 1
- len: 9
- a={1,2,3,4,5}
- b={3,4,5,6,7}
- print(a&b) # 交集
- print(a|b) # 并集
- print(a^b) # 补集
- print(a-b) # 差集
- {3, 4, 5}
- {1, 2, 3, 4, 5, 6, 7}
- {1, 2, 6, 7}
- {1, 2}
- s={1,2,3,4,5,6,7,8,9}
- for item in s:
- print(item)
-
- for index,item in enumerate(s):
- print(index,"----",item)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 0 ---- 1
- 1 ---- 2
- 2 ---- 3
- 3 ---- 4
- 4 ---- 5
- 5 ---- 6
- 6 ---- 7
- 7 ---- 8
- 8 ---- 9
- s={i for i in range(1,10)}
- print(s)
-
- s={i for i in range(1,10) if i%2==1}
- print(s)
- {1, 2, 3, 4, 5, 6, 7, 8, 9}
- {1, 3, 5, 7, 9}