Python学习 - 语法入门:https://blog.csdn.net/wanzijy/article/details/125287855
Python学习 - 数据类型:https://blog.csdn.net/wanzijy/article/details/125341568
Python学习 - 流程控制:https://blog.csdn.net/wanzijy/article/details/125400906
Python学习 - 运算符(操作符):https://blog.csdn.net/wanzijy/article/details/125387919
Python学习 - 列表:https://blog.csdn.net/wanzijy/article/details/125457990
Python学习 - 字典:https://blog.csdn.net/wanzijy/article/details/125567225
Python学习 -元组和集合:https://blog.csdn.net/wanzijy/article/details/125577609
Python学习 - 函数(1):https://blog.csdn.net/wanzijy/article/details/125669084
Python学习 - 函数(2):https://blog.csdn.net/wanzijy/article/details/125739981
Python学习 - 类与对象:https://blog.csdn.net/wanzijy/article/details/125826952
Python学习 - 面向对象之封装:https://blog.csdn.net/wanzijy/article/details/125902925
Python学习 - 面向对象之继承:https://blog.csdn.net/wanzijy/article/details/125963829
Python学习 - 面向对象之多态:https://blog.csdn.net/wanzijy/article/details/127140632
Python - 文件操作(1):https://blog.csdn.net/wanzijy/article/details/127099071
Python - 文件操作(2):https://blog.csdn.net/wanzijy/article/details/127150189
Python学习 - 垃圾回收、特殊方法和模块:https://blog.csdn.net/wanzijy/article/details/126202999
Python学习 - 异常处理:https://blog.csdn.net/wanzijy/article/details/126803672
是不可变的序列
操作方式基本上和列表是一致的
一般当希望数据不改变时,就使用元组,其余情况都使用列表
使用()来创建元组
my_tuple = ()
当元组不是空元组时,括号可以省略
如果不是空元组,它里边至少要有一个“,”
my_tuple1 = 1, 2, 3, 4, 5
my_tuple2 = 1, # 后面的逗号不能省略,不然会被当成是整型
将元组当中的每一个元素都赋值给一个变量
在解包时,变量的数量必须和元组中元素的数量一致。也可以在变量前添加一个 *,这样变量将会获取元组中所有剩余的元素
不能同时出现两个或两个以上的 *
my_tuple4 = 10, 20, 30, 40
a, b, c, d = my_tuple4
print(a, b, c, d) # 10 20 30 40
g, h, *i = my_tuple4
print(g, h, i) # 10 20 [30, 40]
j, *k, l = my_tuple4
print(j, k, l) # 10 [20, 30] 40
*m, n, o = my_tuple4
print(m, n, o) # [10, 20] 30 40
p, q, *r = [1, 2, 3, 4, 5, 6, 7]
print(p, q, r) # 1 2 [3, 4, 5, 6, 7]
列表也能进行解包
p, q, *r = 'hello world'
print(p, q, r) # h e ['l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
常用于交换两个数的值
e = 100
f = 300
e, f = f, e # 交换两个数的值
列表是一个可变对象
注意 id值(在内存中的地址) 的变化,再结合代码中的文字注释进行理解
a = [1, 2, 3]
print(a, id(a)) # [1, 2, 3] 2129651323400
a[0] = 10 # 通过变量去修改对象中的值,不会改变变量所指向的对象
print(a, id(a)) # [10, 2, 3] 2129651323400
a = [4, 5, 6] # 这种操作会改变变量所指向的对象
print(a, id(a)) # [4, 5, 6] 2129651323912
当我们去修改对象时,如果有其他对象也指向该对象,则修改也会在其他变量中体现
a = [1, 2, 3]
b = a
a[0] = 11
print(a) # [11, 2, 3]
print(b) # [11, 2, 3]
一般只有在为变量赋值时才是修改变量,其余的都是修改对象
拓展:
只能存储不可变对象
存储的对象是无序的(不是按照元素的插入顺序保存)
不能出现重复的元素。如果有重复的,只会保存一个,其余的将自动剔除
集合中的元素不能通过索引去操作。如果想用索引来操作的话,要先用 list() 函数转换成列表,然后再通过索引进行操作
s = {} # 创建出来的不是空集合
print(s) # {}
s = {11, 2, 33, 4}
print(s, type(s)) # {33, 2, 11, 4}
s = {1, 1, 1, 2, 3, 7, 4}
print(s) # {1, 2, 3, 4, 7}
s = set()
print(s) # set()
s = set([1, 2, 3, 4])
print(s) # {1, 2, 3, 4}
s = set('hello')
print(s) # {'h', 'o', 'e', 'l'}
s = set({'a': 1, 'b': 2})
print(s) # {'a', 'b'}
add()
s = {1, 2, 3, 'a', 'b'}
s.add(None)
print(s) # {1, 2, 3, None, 'a', 'b'}
update()
s = {1, 2, 3, 'a', 'b'}
s1 = {4, 6, 7, 8, 9}
s.update(s1)
print(s) # {1, 2, 3, None, 4, 6, 7, 8, 9, 'a', 'b'}
s.update((10, 20, 30, 40))
print(s) # {1, 2, 3, None, 4, 6, 7, 8, 9, 10, 40, 20, 'a', 'b', 30}
s.update({10: 'ab', 20: 'cd', 30: 'ef'})
print(s) # {1, 2, 3, None, 4, 6, 7, 8, 9, 10, 40, 100, 200, 300, 'b', 'a', 20, 30}
pop()
s = {1, 2, 3, None, 4, 6, 7, 8, 9, 10, 40, 100, 200, 300, 'b', 'a', 20, 30}
print(s.pop()) # 1
print(s) # {2, 3, None, 4, 6, 7, 8, 9, 10, 40, 100, 200, 300, 'b', 'a', 20, 30}
remove()
s = {2, 3, None, 4, 6, 7, 8, 9, 10, 'b', 40, 100, 200, 300, 'a', 20, 30}
s.remove(2)
print(s) # {3, None, 4, 6, 7, 8, 9, 10, 'b', 40, 100, 200, 300, 'a', 20, 30}
clear()
s = {2, 3, None, 4, 6, 7, 8, 9, 10, 'b', 40, 100, 200, 300, 'a', 20, 30}
s.clear()
print(s) # set()
len()
s = {'a', 'b', 1, 2, 3}
print(len(s)) # 5
in 和 not in
s = {'a', 'b', 1, 2, 3}
print('a' in s) # True
print('a' not in s) # False
copy()
s = {None, 3, 4, 6, 7, 8, 9, 10, 200, 'b', 20, 30, 100, 40, 300, 'a'}
a = s.copy()
print(a) # {None, 3, 4, 6, 7, 8, 9, 10, 200, 'b', 20, 30, 100, 40, 300, 'a'}
做运算时,不会影响原来的集合。而是将运算结果返回
如果a集合中的元素全部都在b集合中出现,那么a集合就是b集合的子集,b集合就是a集合的超集
如果超集b中含有子集a中所有元素,并且b中还有a中没有的元素,则b就是a的真超集,a是b的真子集
s = {1, 2, 3, 4, 5}
s2 = {3, 4, 5, 6, 7}
print(s & s2) # {3, 4, 5}
print(s | s2) # {1, 2, 3, 4, 5, 6, 7}
print(s - s2) # {1, 2}
print(s ^ s2) # {1, 2, 6, 7}
a = {1, 2, 3}
b = {1, 2, 3, 4, 5}
c = {1, 2, 3}
print(a <= b) # True
print(a <= c) # True
print(a < c) # False
print(a < b) # True