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':breakelif last_level=='q':exit_flag=Trueelse:print('enter b/q')else:if choice_district == 'b':breakelif choice_district == 'q':exit_flag = Trueelse:print('enter b/q')else:if choice_city == 'b':breakelif choice_city == 'q':exit_flag = Trueelse:print('enter b/q')else:if choice_province == 'b':breakelif choice_province == 'q':exit_flag = Trueelse: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')
?