语法
应用
old_str = "万过薪月,员序程马黑来,nohtyp学"
new_str = old_str[::-1]
print(f"倒序内容为:{new_str}")
old_str = "万过薪月,员序程马黑来,nohtyp学"
###new_str = old_str[::-1][9:14]
###print(f"新的字符串为:{new_str}")
new_str = old_str[5:10][::-1]
print(f"新的字符串为:{new_str}")
old_str = "万过薪月,员序程马黑来,nohtyp学"
new_str = old_str.split(",")[1].replace("来","")[::-1]
print(f"修改后的字符串为:{new_str}")
set集合
set 集合可以用于去重 因为 set集合内元素不能重复
列表 元组 字符串 集合定义
列表 元组 字符串 都是有序的 支持下标索引
集合是无序的 不支持下标索引
常见函数使用
len (集合) 求集合中元素个数
案例
my_list = ['黑马程序员','传智播客','黑马程序员','传智播客','itheima','itcast','itheima','itcast','best']
empty_set = set()
index = 0
for element in my_list:
print(f"列表中元素为{element}")
empty_set.add(my_list[index])
if index<= len(my_list):
index += 1
print(f"添加后的集合里内容为{empty_set}")
复杂化了 修改后的代码为:
my_list = ['黑马程序员','传智播客','黑马程序员','传智播客','itheima','itcast','itheima','itcast','best']
empty_set = set()
for element in my_list:
empty_set.add(element)
print(f"添加元素到集合后内容为:{empty_set}")
字典
字典定义:
字典相关函数
案例
infomation_dict={
"王力宏":{
"部门": "科技部",
"工资": 3000,
"级别": 1
},
"周杰伦": {
"部门": "市场部",
"工资": 5000,
"级别": 2
},
"林俊杰": {
"部门": "市场部",
"工资": 7000,
"级别": 3
},
"张学友": {
"部门": "科技部",
"工资": 4000,
"级别": 1
},
"刘德华": {
"部门": "市场部",
"工资": 6000,
"级别": 2
}
}
print(f"对员工升职加薪前得到的结果是:{infomation_dict}")
for key in infomation_dict:
if infomation_dict[key]["级别"]== 1:
infomation_dict[key]["工资"]+=1000
infomation_dict[key]["级别"]=2
print(f"对员工升职加薪后的的结果是:{infomation_dict}")