
什么是元组
元组是用小括号定义的,里边是逗号,列表是用方括号  可变序列:列表、字典
'''可变序列 列表 ,字典'''
lst=[10,20,45]
print(id(lst))
lst.append(300)
print(id(lst))
1797129973952
1797129973952
不可变序列,字符串,元组
'''不可变序列,字符串,元组'''
s='hello'
print(id(s))
s=s+'world'
print(id(s))
print(s)
2193108166256
2193108449136
helloworld
元组的创建方式
 第一种创建方式,使用()
'''第一种创建方式,使用()'''
t=('python','world',98)
print(t)
print(type(t))
('python', 'world', 98)
<class 'tuple'>
第二种创建方式,使用内置函数tuple
'''第二种创建方式,使用内置函数tuple'''
tl=tuple(('python','world',98))
print(tl)
print(type(tl))
('python', 'world', 98)
<class 'tuple'>
注:如果只有一个元素不加,则其类型是它本身的类型 空元组的创建方式:
lst=[]
lst1=list()
d={}
d2=dict()
t4={}
t5=tuple()
print('空列表',lst,lst1)
print('空字典',d,d2)
print('空元组',t4,t5)
空列表 [] []
空字典 {} {}
空元组 {} ()
复习:空列表的创建方式、空元组的创建方式
lst=[]
lst1=list()
d={}
d2=dict()
为什么要将元组设计成不可变序列
 上锁 
t=(10,[20,30],9)
print(t)
print(type(t))
print(t[0])
print(t[1])
print(t[2])
(10, [20, 30], 9)
<class 'tuple'>
10
[20, 30]
9
t=(10,[20,30],9)
print(t)
print(type(t))
print(t[0])
print(t[1])
print(t[2])
print(t[0],type(t[0]),id(t[0]))
print(t[1],type(t[1]),id(t[1]))
print(t[2],type(t[2]),id(t[2]))
(10, [20, 30], 9)
<class 'tuple'>
10
[20, 30]
9
10 <class 'int'> 1258127911504
[20, 30] <class 'list'> 1258129666240
9 <class 'int'> 1258127911472
元组的遍历
t=(10,[20,30],9)
for item in t:
print(item)
10
[20, 30]
9
集合的概念与创建
 可以进行增删改 使用花括号,没有value,有key 
集合的相关操作

s={10,20,30,405,60}
print(10 in s)
print(100 in s)
print(10 not in s)
print(100 not in s)
True
False
False
True

集合之间的关系
 
集合的数学操作

- 交集
s1={10,20,30,40}
s2={20,30,50,60,70}
print(s1.intersection(s2))
{20, 30}
&符号等价于intersection交集
print(s1 & s2)
{20, 30}
- 并集
print(s1.union(s2))
{70, 40, 10, 50, 20, 60, 30}
- 差集
print(s1.difference(s2))
{40, 10}
print(s1 -s2)
{40, 10}
- 对称差集
print(s1.symmetric_difference(s2))
{50, 70, 40, 10, 60}
print(s1^s2)
{50, 70, 40, 10, 60}
终于有点数学性的了
集合生成式
 
列表、字典、元组、集合总结

本章知识点总结

英语学习
1 intersection 2 tuple 3 symmetric 
励志语录
社会大抵是一个让人认清现实的地方,它教会人自私,教人冷漠,教人没工夫再去想那些没有结果的感情,教人每天只是认认真真老老实实做好自己的本分事,让人早睡早起!
|