#列表
#列表的方法 print(my_list.index(188)) #返回元素在列表中下标 print(my_list[1].index(("bc"))) my_list.append(18) #在列表尾部追加元素,更新列表 print(my_list) my_list.insert(0, "Best") #插入元素,更新列表 my_list.insert(len(my_list), 12) print(my_list) my_list.extend([100, 1000, 10000, 100000]) #在尾部追加一批新元素 print(my_list)my_list = ["itheima", "itcast", "python"] del my_list[2] #删除对应位置元素 print(my_list) my_list.append("python") print(my_list.pop(2)) #返回值为取出元素,更新列表 print(my_list) my_list = ["itheima", "itcast", "python", "itheima", "itheima", "abc"] my_list.remove("itheima") #移除指定内容的元素,只能删除最前面一个 print(my_list) my_list.clear() #清空列表 print(my_list)my_list = ["itheima", "itcast", "python", "itheima", "itheima", "abc"] print(my_list.count("itheima")) #统计指定内容的元素数量 print(len(my_list))
""" 演示对list列表的循环,使用while 和 for两种方式 """ from operator import indexfrom c601 import my_listdef list_while_func():"""使用while循环遍历列表的演示函数:return: None"""my_list = ["传智教育", "黑马程序员", "Python"]index = 0while index < len(my_list):element = my_list[index]print(f"列表的元素:{element}")index += 1def list_for_func():"""使用for循环遍历列表的演示函数:return: None"""my_list2 = ["I", "like", "Python", "and", "football"]for e in my_list2:print(f"列表的元素:{e}")list_while_func() list_for_func()
""" 练习案例:取出列表中的偶数 遍历一个指定列表,取出其中偶数,并存入一个新列表对象中 """def test_while():"""使用while循环实现:return: None"""my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]index = 0new_list = []while index < len(my_list):if my_list[index]%2 == 0:new_list.append(my_list[index])index += 1print(f"通过while循环,从{my_list}中取出偶数,组成新列表{new_list}。")def test_for():"""使用for循环实现:return:"""my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]new_list = []for e in my_list:if e%2 == 0:new_list.append(e)print(f"通过while循环,从{my_list}中取出偶数,组成新列表{new_list}。")test_while() test_for()
#元组
#元组 """ 演示tuple的定义和操作 """ #定义元组 t1 = (1, "Hello", True, 100, "Hello") t2 = () #空元组 t3 = tuple() #空元组的另一种定义方法 print(f"t1的类型是{type(t1)},内容是{t1}.") print(f"t2的类型是{type(t2)},内容是{t2}.") print(f"t3的类型是{type(t3)},内容是{t3}.")#不涉及修改序列的列表方法,元组同样具备 #但不具备append,insert这些修改类的方法 #通过元素内容查找下标/索引:index() print(t1.index(100)) #统计元素在元组内出现的次数 print(t1.count("Hello")) #元素数量/元组长度 print(len(t1))#元组的while遍历 index = 0 while index < len(t1):print(f"{t1[index]}", end = "***")index += 1#元组的for遍历 for e in t1:print(f"{e}", end = "###")""" 小练习 """ #定义一个元组,记录的一个学生的信息(姓名,年龄,爱好) stu_tuple = ("周婕纶", 11, ["football", "music"])#查询年龄对应下标位置 print(f"年龄对应的下标是{stu_tuple.index(11)}。")#查询学生的姓名 print(f"该学生的姓名是{stu_tuple[0]}。")#删除学生爱好中的football del stu_tuple[2][0]#增加爱好:coding stu_tuple[2].append("coding")print(stu_tuple)
#字符串
#字符串同样也是一个数据容器 from itertools import countmy_str = "itheima and itcast" value1 = my_str[2] value2 = my_str[-2] print(value1, value2)#字符串同样是不可修改的,同样没有list那样的修改的方法print(my_str.index("a")) #同样只输出第一个a的索引 print(my_str.index("ca")) #也可以查字符串#replace()字符串的替换,并不是修改原字符串,而是得到了一个新字符串 my_new_str = my_str.replace("it", "编程") print(f"原字符串:{my_str}替换后得到新字符串:{my_new_str}。")#split(),字符串的分割,按照指定分隔符将原字符串分成多个字符串并存入列表对象中 my_str = "hello python itheima itcast" new_str_list = my_str.split(" ") print(f"原字符串仍然不变:{my_str},进行split分割后得到新列表{new_str_list}")#strip()字符串的规整 my_str = " iteheima and itcast " new_my_str = my_str.strip() #不传入参数,默认去除首尾空格 print(f"{my_str}通过strip()去除后得到新字符串为{new_my_str}") my_str = "12 iteheima and itcast 21" new_my_str = my_str.strip("12") #参数中的字符都将被首尾处删除 print(f"{my_str}通过strip(\"12\")去除后得到新字符串为{new_my_str})")#count()统计字符串中指定字符串出现次数 n_count = my_str.count("it") print(f"字符串it在原字符串{my_str}中出现的次数是{n_count}次。")""" 练习 """ #给定一个字符串 test_str = "itheima itcast boxuegu"#统计其中有多少个it字符 count_s = test_str.count("it") print(f"字符串{test_str}中共有{count_s}个it字符。")#将字符串内的空格,全部替换为“|” new_str = test_str.replace(" ", "|") print(f"字符串{test_str}被替换空格后得到{new_str}。")#将新字符串按照|进行分割 new_str_list = new_str.split("|") print(f"字符串{new_str}按照|分割后得到列表{new_str_list}。")
#序列的切片
#序列的切片 my_list = [0, 1, 2, 3, 4, 5, 6] print(my_list[1:4]) print(tuple(my_list)[0:]) print(my_list[::2]) print(my_list[::-1]) print(my_list[-4:-6:-1]) print(my_list[3:1:-1])""" 练习 “万过薪月,员序程马黑来,nohtyp学” 使用学过的任何方式,得到字符串黑马程序员 """ s = "万过薪月,员序程马黑来,nohtyp学" #方法1 index1 = s.index("员") index2 = s.index("黑") new_s = s[index2:(index1-1):-1] print(new_s) #方法2 new_s2 = s.split(",")[1][::-1][1:] print(new_s2)