python黑马&元组&字符串&集合&字典&函数&转换

1、元组
t1 = (1, "sdd", True)
#t2 = tuple("sdf", 2, 4)
#tuple expected at most 1 argument, got 2
#tuplr只能接受一个参数
t2 = tuple ((1, "dsc", 3))
print(t2)
t3 = ((1, 2, 3,), (4, 5, 6), (1, 2, 3))
print(t3[1][2])

print(len(t3)) # 长度
print(t3.count((1, 2, 3)))

#通过列表修改元组
t4 = (1, 2, [3, 4])
t4[2][0] = 1
print(t4)
2、字符串

python黑马&元组&字符串&集合&字典&函数&转换_第1张图片

# 字符串
m_str = " sahjcbhagjhsjhc "
m_str1 = "dsffv"
#替换
new_m_str = m_str.replace("ah", "aaa")
print(new_m_str)
# 分割
my_str_list = m_str.split('a')
print(my_str_list)
#split 默认去掉前后的空格
m_str2 = m_str.split(" ")
print(m_str2)
# ['', 'sahjcbhagjhsjhc', '']
 3、集合

python黑马&元组&字符串&集合&字典&函数&转换_第2张图片

# 集合
# 不允许重复且顺序无法保证
my_set = {1, 2, 3, 4, 5, 1, 2, 6}
print(my_set)
# 添加新元素
my_set.add (6)
print(my_set)
my_set.add (8)
print(my_set)
# 移除
my_set.remove(6)
print(my_set)
# 随机取出元素
print (my_set.pop ())
# 清空集合 clear()
# 取差级
my_set1 = {1, 2, 3, 8, 9}
my_set2 = my_set.difference(my_set1)
print(my_set2)
#  合并
my_set3 = my_set.union(my_set1)
print(my_set3)

# 集合不支持下标索引

for element in my_set :
    print(element)
4、字典
# 字典
# 不能使用下标索引
my_dict = {"jas":3, "sad":4}
print(my_dict["jas"])
# 嵌套
m_dict = {
    "www":{"aa":2, "sas":32},
    "dfnk":{"ae":32, "ds":21},
    "sasw":{"asd":23, "as":1}
}
print(m_dict["www"]["aa"])

python黑马&元组&字符串&集合&字典&函数&转换_第3张图片

5、总结

python黑马&元组&字符串&集合&字典&函数&转换_第4张图片

python黑马&元组&字符串&集合&字典&函数&转换_第5张图片

python黑马&元组&字符串&集合&字典&函数&转换_第6张图片

6、容器排序 sorted()

你可能感兴趣的:(python,开发语言)