学习python的第五天

101 阅读6分钟

元组的基本操作

neme = ('周杰伦', 11, ['football', 'music'])

index = neme.index(11)

index_name = neme.index('周杰伦')

neme[2][0]='coding'

print(index)

print(index_name)

print(neme)

字符串的基本操作

my_str ='itheima and itcast' value = my_str[2] value2 = my_str[-1] print(f"从字符串{my_str}中取下表为2的元素,值是{value},以及取下标为-1的元素值是:{value2}") input = my_str.index("and") print(f"在字符串中{my_str}中查找and,起始下标是{input}") my_str1=my_str.replace('itheima and itcast','黑马程序员') print(my_str1) my_str ='itheima and itcast' partition = my_str.split(' ') print(partition) my ='12 1,2,3, 12' my1 = my.strip() print(f"字符串my的内容是{my},去除空格的字符串内容是{my1}") my2 = my.strip("12") print(f"字符串my的内容是{my},去除字符串的内容是{my2}") my_str1 = '1,2,3,4,5,6,7,1,2,3,4,5,6,7,1,1,1,1,1,1' len_str =len(my_str1) print(f'字符串{my_str1}的长度是{len_str}') len_str2 = my_str1.count('1') print(f'字符串my_str1当中有{len_str2}个1') my_str_ ='itheima and itcast' input = 0 while input < len(my_str_): print(my_str_[input]) input += 1 for i in my_str_: print(i)

str_it = 'itheima itcast boxuegu' str_count = str_it.count("it") print(f"字符串中it出现的次数是{str_count}")

str_replace = str_it.replace(' ', '|') print(str_replace)

str_split = str_it.split("|") print(f'字符串按照|分割之后是{str_it}')

print(str_split)

对列表进行切片

list = [0,1,2,3,4,5,6,7] list_xu = list[1:4] print(list_xu) list_lie = list[3:1:-1] print(list_lie)

对元组进行切片

tuple = (0,1,2,3,4,5,6,7,8,9,10) tuple_xu = tuple[:] print(tuple_xu) tuple_lie = tuple[::-2] print(tuple_lie)

对字符串进行切片

str = '0123456789' str_xu = str[::2] print(str_xu) str_lie = str[::-1] print(str_lie)

切片的小练习

str1 = "万过薪月,员序程马黑来,nohtyp学" str2 = str1[::-1] str3 = str2[9:14] print(f"反转的字符串是{str2},切片的字符串是{str3}") str4 = "万过薪月,员序程马黑来,nohtyp学" str5 = str4[5:10] str6 = str5[::-1] print(f"切片的字符串是{str6}") str7 = "万过薪月,员序程马黑来,nohtyp学" str8 = str7.split(",")[1].replace("来",'')[::-1] print(str8)

# 定义集合

my_set = {'传智教育','黑马程序员','itheima','传智教育','黑马程序员','itheima','传智教育','黑马程序员','itheima','传智教育','黑马程序员','itheima',} my_set_empty = set() print(f"my_set的内容是:{my_set},类型是{type(my_set)}") print(f"my_set_empty的内容是:{my_set_empty},类型是{type(my_set_empty)}") my_set.add("python") my_set.add("传智教育") print(f'my_set添加元素后的结果是:{my_set}') my_set.remove("传智教育") print(f'my_set移除元素后的结果是:{my_set}') my1 = my_set.pop() print(my1) print(my_set) print(f'my_set的长度是:{len(my_set)}')

print(f'my_set的元素是:{my_set}')

清空集合

my_set.clear() print(f'my_set清空后的结果是:{my_set}') #取两个集合的差集 my_set1 = {1,2,3} my_set2 = {1,5,6}

my_set3 = my_set1.difference(my_set2) print(my_set1) print(my_set2) print(f'my_set1和my_set2的差集是:{my_set3}')

# 消除两个集合的差集

my_set1 = {1,2,3} my_set2 = {1,5,6} my_set1.difference_update(my_set2) print(f'消除差集后,集合1的结果是:{my_set1}') print(f'消除差集后,集合2的结果是:{my_set2}')

两个集合合并

my_set1 = {1,2,3} my_set2 = {1,5,6} my_set3 = my_set1.union(my_set2) print(f'两个集合合并的结果是:{my_set3}') #统计集合元素数量 my_set1 = {1,2,3} print(f'my_set1的元素数量是:{len(my_set1)}')

集合得的遍历

my_set1 = {1,2,31,2,31,2,31,2,31,2,31,2,31,2,31,2,31,2,31,2,3} for item in my_set1: print(f"集合的元素有:{item}")

信息去重

my_list = ['黑马程序员','传智播客','黑马程序员','传智播客','itheima','itcast','itheima','itcast','best'] my_set = set(my_list) print(f"my_set的内容是:{my_set},类型是{type(my_set)}") my_list = ['黑马程序员','传智播客','黑马程序员','传智播客','itheima','itcast','itheima','itcast','best'] my_set = set() for item in my_list: my_set.add(item) print(f"列表的内容是:{my_list},类型是{type(my_list)}") print(f'通过for循环后,得到的集合对象是{my_set}') print(type(my_set))

定义字典

my_dict1={'王力宏':99,'周杰伦':88,'蔡依林':77} my_dict2={} my_dict3=dict() print(f'字典一的内容是{my_dict1},类型:{type(my_dict1)}') print(f'字典二的内容是{my_dict2},类型:{type(my_dict2)}') print(f'字典三的内容是{my_dict3},类型:{type(my_dict3)}')

定义重复key字典

my_dict4={'王力宏':99,'周杰伦':88,'蔡依林':77,'周杰伦':99} print(f'字典四的内容是{my_dict4},类型:{type(my_dict4)}')

从字典基于key获取value

print(f'王力宏的考试分数是{my_dict1["王力宏"]}') print(f'蔡依林的考试分数是{my_dict1["蔡依林"]}')

字典的嵌套

my_dict5={'王力宏':{'数学':99,'语文':98,'英语':97},'周杰伦':{'数学':89,'语文':88,'英语':87},'蔡依林':{'数学':79,'语文':78,'英语':77}} print(f'字典五的内容是{my_dict5},类型:{type(my_dict5)}') print(f'蔡依林的数学分数是{my_dict5["蔡依林"]["数学"]}')

从嵌套的字典中获取数据 看周杰伦的语文

print(f'周杰伦的语文分数是{my_dict5["周杰伦"]["语文"]}') print(f'蔡依琳的语文分数是{my_dict5["蔡依林"]["语文"]}')

字典的常用操作 新增元素

my_dict6={'王力宏':99,'周杰伦':88,'蔡依林':77} my_dict6['张信哲']=66 print(f'字典六的经过新增元素后,结果是:{my_dict6},类型:{type(my_dict6)}')

更新元素

my_dict6['王力宏']=88 print(f'字典六的经过更新元素后,结果是:{my_dict6},类型:{type(my_dict6)}')

删除元素

my_dict6.pop('张信哲') print(f'字典六的经过删除元素后,结果是:{my_dict6},类型:{type(my_dict6)}')

清空元素

my_dict6.clear()

print(f'字典六的经过清空元素后,结果是:{my_dict6},类型:{type(my_dict6)}')

获取全部key

print(f'字典六的key是:{my_dict6.keys()}')

遍历字典

for key in my_dict6.keys(): print(f'key是{key},value是{my_dict6[key]}')

统计字典内的元素数量

print(f'字典六的元素数量是:{len(my_dict6)}')

字典的练习

neme = {'王力宏':{"部门":"科技部","工资":3000,"级别":1,},'周杰伦':{"部门":"市场部","工资":5000,"级别":2,},'林俊杰':{"部门":"行政部","工资":7000,"级别":3,},'张信哲':{"部门":"财务部","工资":4000,"级别":1,}} print(f'全体员工当前信息如下:{neme}')

通过for循环对级别为1的员工工资提升1000,并且级别上升一级

for name,info in neme.items(): if info["级别"]==1: info["工资"]+=1000 info["级别"]+=1 print(f'员工工资信息如下:{neme}')