&
s = {1,2,3,4,5}
s2 = {3,4,5,6,7}
# & 交集运算
result = s & s2
print(result) # {3, 4, 5}
1
2
3
4
5
|
s = {1,2,3,4,5}
s2 = {3,4,5,6,7}
# | 并集运算
result = s | s2
print(result) # {1,2,3,4,5,6,7}
1
2
3
4
5
s = {1,2,3,4,5}
s2 = {3,4,5,6,7}
# - 差集
result = s - s2
print(result) # {1, 2}
1
2
3
4
5
^
s = {1,2,3,4,5}
s2 = {3,4,5,6,7}
# ^ 异或集 获取只在一个集合中出现的元素
result = s ^ s2
print(result) # {1, 2, 6, 7}
1
2
3
4
5
6
<=
<= 检查一个集合是否是另一个集合的子集
如果a集合中的元素全部都在b集合中出现,那么a集合就是b集合的子集,b集合是a集合超集
a = {1,2,3}
b = {1,2,3,4,5}
result = a <= b # True
result = {1,2,3} <= {1,2,3} # True
result = {1,2,3,4,5} <= {1,2,3} # False
1
2
3
4
5
<
< 检查一个集合是否是另一个集合的真子集
如果超集b中含有子集a中所有元素,并且b中还有a中没有的元素,则b就是a的真超集,a是b的真子集
result = {1,2,3} < {1,2,3} # False
result = {1,2,3} < {1,2,3,4,5} # True
1
2
大于和>=
# >= 检查一个集合是否是另一个的超集
# > 检查一个集合是否是另一个的真超集
1
2
通用方法
包含字符串(str) 元组(tuple) 列表(list)
切片操作
ps: 列表 元组 字符串 均可切片 集合和字典不可切片
数据[起始索引:结束索引:步长]
name = "abcdefg"
print(name[2:5:1]) # cde
print(name[2:5]) # cde
print(name[:5]) # abcde
print(name[1:]) # bcdefg
print(name[:]) # abcdefg
print(name[::2]) # aceg
print(name[:-1]) # abcdef, 负1表示倒数第一个数据print(name[-
4:-1]) # def
print(name[::-1]) # gfedcba
1
2
3
4
5
6
7
8
9
10
len()
str_data = "hello python"
print(len(str_data)) # 12: 字符中字符个数(包含空格)
list_data = ["python", "java"]
print(len(list_data)) # 2: 列表中元素个数
tuple_data = ("admin", 123456, 8888)
print(len(tuple_data)) # 3: 元组中元素个数
dict_data = {"name": "tom", "age": 18, "gender": "男"}
print(len(dict_data)) # 3: 字典中键值对的个数
1
2
3
4
5
6
7
8
加减乘除 不是都是哦
字符串 和 列表 和 元组 可以 + * 字典和集合不行
in or not in
max() min()
count() 对 字典和集合不适用
index_1 = '123'
print(index_1.count('1')) #1
index_1 = [1,2,3]
print(index_1.count(1)) #1
index_1 = (1,2,3)
print(index_1.count(1)) #1
————————————————
版权声明:本文为CSDN博主「BRAVE_Fighting」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/shijianzhihai/article/details/126682302