python基础第二天的课程,主要内容列表,元祖,字典,字符串等主要内容
列表
定义列表
>>> names = ["ZhangSan","LiSi","WangWu","ZhaoLiu",["ZhangXin","MengDong"],"ZongQi","HeiBa","DongJiu"] >>> print(names) ['ZhangSan', 'LiSi', 'WangWu', 'ZhaoLiu', ['ZhangXin', 'MengDong'], 'ZongQi', 'HeiBa', 'DongJiu']
列表取值
>>> names[0] #names列表元素从左往右,从0开始数 'ZhangSan' >>> names[4][0] 'ZhangXin'
列表切片
>>> print(names[0]) ZhangSan >>> print(names[0:]) #从第一个元素取到最末 ['ZhangSan', 'LiSi', 'WangWu', 'ZhaoLiu', ['ZhangXin', 'MengDong'], 'ZongQi', 'HeiBa', 'DongJiu'] >>> print(names[-1]) #取最后一个元素 DongJiu >>> print(names[:-1]) #从第一个元素取到最后一个(不包含最后一个) ['ZhangSan', 'LiSi', 'WangWu', 'ZhaoLiu', ['ZhangXin', 'MengDong'], 'ZongQi', 'HeiBa'] >>> print(names[-2:]) #取最后两个元素 ['HeiBa', 'DongJiu'] >>> print(names[:4]) #取前4个元素 ['ZhangSan', 'LiSi', 'WangWu', 'ZhaoLiu'] >>> print(names[-4:]) #取后四个元素 [['ZhangXin', 'MengDong'], 'ZongQi', 'HeiBa', 'DongJiu'] >>> print(names[::2]) #从头开始取,2代表,每隔一个取一个。 如果是3,每两个取一个,以此类推 ['ZhangSan', 'WangWu', ['ZhangXin', 'MengDong'], 'HeiBa']
列表的操作
>>> names.append("DaWa") #在列表内最后追加 >>> print(names) ['ZhangSan', 'LiSi', 'WangWu', 'ZhaoLiu', ['ZhangXin', 'MengDong'], 'ZongQi', 'HeiBa', 'DongJiu', 'DaWa']
names.insert(1,"ErWa") #在下标位置为1的元素前插入 >>> print(names) ['ZhangSan', 'ErWa', 'LiSi', 'WangWu', 'ZhaoLiu', ['ZhangXin', 'MengDong'], 'ZongQi', 'HeiBa', 'DongJiu', 'DaWa']
>>> names[1]="SanWa" >>> print(names) ['ZhangSan', 'SanWa', 'LiSi', 'WangWu', 'ZhaoLiu', ['ZhangXin', 'MengDong'], 'ZongQi', 'HeiBa', 'DongJiu', 'DaWa']
>>> del names[0] #删除元素下标位置的数据 >>> print(names) ['SanWa', 'LiSi', 'WangWu', 'ZhaoLiu', ['ZhangXin', 'MengDong'], 'ZongQi', 'HeiBa', 'DongJiu', 'DaWa'] >>> names.remove("DaWa") #删除指定数据 >>> print(names) ['SanWa', 'LiSi', 'WangWu', 'ZhaoLiu', ['ZhangXin', 'MengDong'], 'ZongQi', 'HeiBa', 'DongJiu'] >>> names.pop() #删除最后一个数据 'DongJiu' >>> print(names) ['SanWa', 'LiSi', 'WangWu', 'ZhaoLiu', ['ZhangXin', 'MengDong'], 'ZongQi', 'HeiBa']
>>> print(names) ['SanWa', 'LiSi', 'WangWu', 'ZhaoLiu', ['ZhangXin', 'MengDong'], 'ZongQi', 'HeiBa'] >>> names2=["1","2","3","4","5","6","LiSi"] >>> names.extend(names2) #在names列表数据后面追加names2的数据 >>> print(names) ['SanWa', 'LiSi', 'WangWu', 'ZhaoLiu', ['ZhangXin', 'MengDong'], 'ZongQi', 'HeiBa', '1', '2', '3', '4', '5', '6', 'LiSi']
>>> names1 = ['one','two','three','four',["five","six"],7,8,9,] >>> names2 = names1.copy() >>> print(names2) ['one', 'two', 'three', 'four', ['five', 'six'], 7, 8, 9] >>> names2[4][1]= "none" #修改name2列表中的列表数据后,names1中的值也跟着修改 >>> print(names1) ['one', 'two', 'three', 'four', ['five', 'none'], 7, 8, 9] >>> print(names2) ['one', 'two', 'three', 'four', ['five', 'none'], 7, 8, 9]
>>> names=["LiSi","1","2","3","4","5","6","LiSi"] >>> names.count("LiSi") 2
>>> names = ['one','two','three','four',"7","8","9"] >>> names.sort() >>> print(names) #按ASCII编码排序 ['7', '8', '9', 'four', 'one', 'three', 'two']
>>> names = ['one','two','three','four',"7","8","9"] >>> names.reverse() >>> print(names) ['9', '8', '7', 'four', 'three', 'two', 'one']
>>> names ['9', '8', '7', 'four', 'three', 'two', 'one'] >>> names.index("four") #获取该元素下标值 3
元组
元组tuple和列表十分类似,只不过元组和字符串一样是不可变的,即你不能修改元组。
定义元组
>>> t_name = ("zhang","li","wang","zhao","sun") >>> print(t_name) ('zhang', 'li', 'wang', 'zhao', 'sun')
元组操作
统计
>>> t_name = ("zhang","li","wang","zhao","sun") >>> t_name.count("sun") 1
获取下标
>>> t_name.index("wang") 2
购物车练习
- 启动程序后,让用户输入工资,然后打印商品列表
- 允许用户根据商品编号购买商品
- 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
- 可随时退出,退出时,打印已购买商品和余额
1 shoping_list = [ 2 ('iphone',5800), 3 ('Mac Pro', 16000), 4 ("bike", 800), 5 ("python book", 58), 6 ] 7 curt_list = [] #购物车列表 8 salary = input("你的工资是多少:") 9 if salary.isdigit(): #判断是否是整数 10 salary = int(salary) #转整型 11 while True: 12 for index,item in enumerate(shoping_list): #循环打印商品列表 13 # print(shoping_list.index(item),item) 14 print(index,item) 15 choice_number = input("请输入你要选择的编号:") 16 if choice_number.isdigit(): 17 choice_number = int(choice_number) 18 if choice_number < len(shoping_list) and choice_number >=0: #判断选择编号是否存在 19 p_item = shoping_list[choice_number] #选择商品 20 if p_item[1] <= salary: #判断 商品价格小于等于工资 购买,否则提示余额不足 21 curt_list.append(p_item) #加入购物车 22 salary -=p_item[1] #剩余余额 23 print("