day2-列表、元组、字典、字符串
1.列表(list)
names=['悟空','艾玛','克林','龟仙人','天津饭','饺子','乌龟'] print(names)
---》列表,然后打印。
names=['悟空','艾玛','克林','龟仙人','天津饭','饺子','乌龟'] # list section section1=names[0] #下标从0开始 #print(section1) #结果为'悟空' seciton2=names[1:5] #取下标1至下标5之间的切片,包括1,但不包括5 #print(seciton2) #['艾玛', '克林', '龟仙人', '天津饭'] section3=names[1:-1] #取下标1至下标-1(乌龟)之间的切片,包括1,但不包括-1 #print(section3) #['艾玛', '克林', '龟仙人', '天津饭', '饺子'] ####以下是两种从头截取##### section4=names[0:3] #从头截取,到下标3位置,不包括3 section5=names[:3] #从头截取,到下标3位置,不包括3 #print(section4) #['悟空', '艾玛', '克林'] #print(section5) #['悟空', '艾玛', '克林'] ####以下试试截取到末尾##### section6=names[3:] #从3截取到末尾 #print(section6) #['龟仙人', '天津饭', '饺子', '乌龟'] section7=names[3:-1] #不包括-1,所以这样无法截取到末尾 #print(section7) #['龟仙人', '天津饭', '饺子'] ####加大运动量,我要跳着前进##### section8=names[0::2] #从头到尾,隔一取一 #print(section8) #['悟空', '克林', '天津饭', '乌龟'] section9=names[::2] #效果同上 #print(section9) #['悟空', '克林', '天津饭', '乌龟'] section10=names[:] #all print(section10) #['悟空', '艾玛', '克林', '龟仙人', '天津饭', '饺子', '乌龟']
---》以上为截取示例。
#############接下来试试增、删、改################
names=['悟空','艾玛','克林','龟仙人','天津饭','饺子','乌龟'] # 新增 # names.append('比克大魔王') #我大魔王来了,哈哈哈 # print(names) #['悟空', '艾玛', '克林', '龟仙人', '天津饭', '饺子', '乌龟', '比克大魔王'] # names.insert(3,'安琪') #龟仙人,美女来了。。。 # print(names) #['悟空', '艾玛', '克林', '安琪', '龟仙人', '天津饭', '饺子', '乌龟', '比克大魔王'] #修改 # names[2]='贝吉塔' #离我家艾玛远一点 # print(names) #['悟空', '艾玛', '贝吉塔', '龟仙人', '天津饭', '饺子', '乌龟'] #删除 # del names[-2] #饺子被吃掉啦。。。 # print(names) #['悟空', '艾玛', '克林', '龟仙人', '天津饭', '乌龟'] # names.remove('龟仙人') #走开好色仙人 # print(names) #['悟空', '艾玛', '克林', '天津饭', '饺子', '乌龟'] # names.pop() #可怜的乌龟,没存在感,消失了都没人知道 # print(names) #['悟空', '艾玛', '克林', '龟仙人', '天津饭', '饺子']
#############扩展、拷贝、统计、排序&反转################
#扩展 kids=['悟饭','特兰克斯','撒旦'] names.extend(kids) #熊孩子来 print(names) #['悟空', '艾玛', '克林', '龟仙人', '天津饭', '饺子', '乌龟', '悟饭', '特兰克斯', '撒旦'] #拷贝(浅浅的copy,后面会介绍deepcopy) n_copy=names.copy() n_copy[0]='大圣' names[0]='至尊宝' print('n_copy:',n_copy) print('names:',names) #统计,只统计第一层 name_count=names.count('乌龟') print(name_count) #排序 names[-1]='1' names[-2]='b' names[-3]='@' names.sort() print(names) #反转 names.reverse() print(names)
#############列表循环、获取下标################
#列表循环 for n in names: print(n) #获取下标(只返回找到的第一个下标) name=names.index('克林') print(name)
#############我要深深的copy################
names=['悟空','艾玛','克林','龟仙人','天津饭','饺子','乌龟'] #拷贝 # n_copy=names.copy() # n_copy[0]='大圣' # names[0]='至尊宝' # print('n_copy:',n_copy) # print('names:',names) # immortals=['天神','界王','大界王','界王神','乌龟'] # names.insert(1,immortals) #print(names) ########套了一层列表,继续试试copy########### # n_copy=names.copy() # names[0]='至尊宝' #一层正常copy # names[1][0]='bobo' #修改二层列表,打印看看,咋都变了呢? # n_copy[1][0]='喵喵' #修改二层列表,打印看看,咋都变了呢? # print('n_copy:',n_copy) # print('names:',names) #####浅copy,只复制一层,子列表未被复制一份,所以一个修改后,两者都变,可以理解为指向同一内存地址 ########我想深深的copy咋整?############ # import copy # n_copy=copy.deepcopy(names) # names[1][0]='bobo' #deepcopy,达到了想要的效果 # n_copy[1][0]='喵喵' #deepcopy,达到了想要的效果 # print('n_copy:',n_copy) # print('names:',names)
2.元组(tuple)
names=('悟空','艾玛','克林','龟仙人','天津饭','饺子','乌龟','饺子') name_count=names.count('饺子') #tuple count name_index=names.index('天津饭') #tuple index print(names) #('悟空', '艾玛', '克林', '龟仙人', '天津饭', '饺子', '乌龟', '饺子') print('count:',name_count) #count: 2 print('index of 天津饭:',name_index) #index of 天津饭: 4 # tuple loop for n in names: print(n)
---》使用类似列表
3.字典(dict)
'''
字典特性
1.dict是无序的
2.key值必须是唯一的
'''
infos={
'stu1101':'TengLan Wu',
'stu1102':'LuoLa LongZe',
'stu1103':'MaryYa XiaoZe'
}
info=infos['stu1101'] #key-value取值
infos['stu1104']='Jinkong Cang' #新增元素
infos['stu1101']='武藤兰' #修改
#infos.pop('stu1104') #标准删除姿势
#del infos['stu1104'] #换个姿势删除
#infos.popitem() #随机删除一个
#标注查找
# if 'stu1101' in infos:
# print('Yes,it is exsit.')
# else:
# print('not exsit')
#获取
# print(infos.get('stu1101')) #获取
# print(infos['stu1101']) #同上
##########But,if key is non-exsitent!#############
# print(infos.get('stu1106')) #stu1106不存在,返回none
# print(infos['stu1106']) #会报错KeyError: 'stu1106'
---》最简单的字典
#多级字典嵌套及操作 addresses={ '江苏':{ '徐州':{ '铜山':{ '汉王':{}, '大庙':{}, '大彭':{} }, '泉山':{ 'Town1': {}, 'Town2': {}, 'Town3': {} }, '鼓楼':{} }, '南京':{}, '苏州':{} }, '北京':{}, '上海':{} } # #第一层 # print('--------L1--------') # for province in addresses: # print(province) # #第二层 # print('--------L2--------') # for city in addresses['江苏']: # print(city) # #第三层 # print('--------L3--------') # for district in addresses['江苏']['徐州']: # print(district) # #第四层 # print('--------L4--------') # for town in addresses['江苏']['徐州']['铜山']: # print(town) ###############下面来实现多级菜单##################### exit_flag=False while not exit_flag: for province in addresses: print(province) choice_province=input('choice province--->>') if choice_province in addresses: while not exit_flag: for city in addresses[choice_province]: print(city) choice_city=input('choice city--->>') if choice_city in addresses[choice_province]: while not exit_flag: for district in addresses[choice_province][choice_city]: print(district) choice_district=input('choice district--->>') if choice_district in addresses[choice_province][choice_city]: while not exit_flag: for town in addresses[choice_province][choice_city][choice_district]: print(town) last_level=input('this is the last level--->>') if last_level=='b': break elif last_level=='q': exit_flag=True else: print('enter b/q') else: if choice_district == 'b': break elif choice_district == 'q': exit_flag = True else: print('enter b/q') else: if choice_city == 'b': break elif choice_city == 'q': exit_flag = True else: print('enter b/q') else: if choice_province == 'b': break elif choice_province == 'q': exit_flag = True else: print('enter b/q')
---》多级字典嵌套及操作,循环打印三级菜单
4.字符串(string)
#string var1='Hello World!' # print(var1) # print(var1[0]) # print(var1[-1]) # print(var1[:]) #不可修改--如下(TypeError: 'str' object does not support item assignment) # var1[0]='HH' # print(var1) # name1='albert' # print(name1.capitalize()) #首字母大写 # name2='Tom' # print(name2.casefold()) #首字母小写 # name3='Spring' # print(name3.center(20,'-')) #共20个字符,name3放中间 # name4='my name is nidaye' # print(name4.count('i')) #统计字符串中‘’的个数 # print(name4.encode()) #将字符串编码改成bytes格式 # print(name4.endswith('ye')) #判断字符串name4是否以‘ye’结尾,是返回True # print('Alex\tli'.expandtabs(20)) #'\t'位置以20个空格填充 # print(name4.find('m')) #返回第一个找回的‘m’,找到返回其索引,否则返回-1 #字符串传参的若干种方法 # msg1='my name is %s, and age is %d' %('tony1',11) # print(msg1) # msg2='my name is {}, and age is {}'.format('tony2',22) # print(msg2) # msg3='my name is {name}, and age is {age}'.format(name='tony3',age=33) # print(msg3) # msg4='my name is {0}, and age is {1}'.format('tony4',44) # print(msg4) #字典传值 # msg5='my name is {name}, and age is {age}'.format_map({'name':'tony5','age':55}) # print(msg5) # print(msg5.index('n')) #返回第一个'n'所在的索引 # print(msg5.partition('is')) #把‘is’、‘is’之前、‘is’之后,作为元组的三个元素,构成一个元组? # print(msg5.replace('m','M',1)) #把第一个'm',改为'M' # # print('9'.isdigit()) #判断一个字符串是否为数字 # print(msg5.isalnum()) #判断一个字符串是否为数字和字母 # # print('|'.join(['n','d','y'])) # # intab='aeiou' #this is the string having actual characters.真实 # outtab='12345' #this is the string having corresponding mapping character.映射 # trantab=str.maketrans(intab,outtab) #建立对应关系 # # str='This is the string example...wow' # print(str.translate(trantab)) #以映射数字替换相应字母 name5='NiDaYe' # print(name5.swapcase()) #大小写变换 # print(name5.zfill(20)) #打印20个字符,不够以0填充 # # print(name5.rjust(20,'-')) #共20个字符,右对齐,不够左侧填充‘-’ # print(name5.ljust(20,'-')) #共20个字符,左对齐,不够右侧填充‘-’ # # print(name5.isidentifier()) #判断一个字符串是否符合命名规则
5.简易购物车
product_list=[ ('Ipone',5000), ('Coffee',35), ('Bike',2000), ('Book',50), ('Mac pro',9800), ('Watch',500) ] #print(product_list[0][1]) #print(len(product_list)) cart=[] salary=input('enter you salary--->>') if salary.isdigit(): salary=int(salary) while True: for index,item in enumerate(product_list): print(index,item) choice_product=input('choice you product--->>') if choice_product.isdigit(): choice_product=int(choice_product) if choice_product>=0 and choice_product<len(product_list): salary=salary-product_list[choice_product][1] if salary>0: cp_item=product_list[choice_product] cart.append(cp_item) print('you have choice %s to you cart,you current balance is %d'%(cp_item,salary)) else: print('没钱买个毛线啊!') elif choice_product=='q': for i in cart: print('you cart:',i) print('you current balance is:',salary) exit() else: print('enter num') else: input('enter num')