Before we learned the list and the dictionary, today we look at a similar type of meta-ancestor as a list.
Preview:
Simple Shopping Cart
To print the Product details, users enter the product name and purchase number, then the product name, price, purchase number added to the shopping list, if the input is empty or other illegal input requires the user to re-enter
msg_dic={
' Apple ': 10,
' Tesla ': 100000,
' Mac ': 3000,
' Lenovo ': 30000,
' Chicken ': 10,
}
One, tuple (tuple)
Why should there be a tuple? ===> to save multiple values, the tuple is immutable in comparison to the list, mainly for reading
definition: Similar to the list type, except [] for (), the tuple is immutable, so it can be used as a dictionary key
1 t= (1,[1,3],'sss'#t=tuple ((1,[1,3], ' SSS ', ()))2 Print(type (t))
Tuples so can be used as a dictionary key
1 # tuples can be a key to a dictionary 2 d={():'Egon'}3 print(D,type (d), d[( )])
Common methods:
1 #why should have tuples, store multiple values, tuples immutable, more is used to do queries2T= (1,[1,3],'SSS', ())#T=tuple ((1,[1,3], ' SSS ', ()))3 Print(Type (t))4 5 #tuples can be a key to a dictionary6D={(a):'Egon'}7 Print(D,type (d), d[()])8 9 #IndexTenGoods= ('iphone','Lenovo','sanxing','Suoyi') One Print(Goods.index ('iphone')) A Print(Goods.count ('iphone'))#Statistics - - #slices theGoods= ('iphone','Lenovo','sanxing','Suoyi') - Print(Goods[1:3]) - - #length + Print(len (goods)) - + #In : AGoods= ('iphone','Lenovo','sanxing','Suoyi') at Print('iphone' inchgoods) - - #add: The tuple itself is immutable, but the inner element can be a mutable type -T= (1,['a','b'],'SSS', ())#T=tuple ((1,[1,3], ' SSS ', ())) -t[1][0]='A' - Print(t) in #t[1]= ' aaaaaaaaaaaa ' #报错
Add: The tuple itself is immutable, but the inner element can be a mutable type
1 t= (1,['a','b'),'sss' #t=tuple ((1,[1,3], ' SSS ', ()))2 t[1][0]='A' 3print(t)4# t[1]= ' aaaaaaaaaaaa ' #报错
Second, assign the value
1x=102 3 #Chained Assignment4a=b=c=d=e=f=105 Print(a,b,c,d,e,f)6 7 #Increment Assignment8X+=19X-=1Ten ... One A #two worth of interchangeable -x=10 -y='a' the #fa Yi -temp=x -x=y -y=Temp + Print(x, y) - #Method Two +x,y=y,x A Print(x, y) at - - #Decompression of Values -msg='Hello' -l=[1,2,3] - inA,b,c,d,e=msg - Print(a,b,c,d,e) to +A,_,_,_,e=msg -A,*_,e=msg the Print(a) * Print(e) $ Panax Notoginsengdic={'a': 1,'b': 2,'C': 3} -x,y,z=DiC the Print(x, y, z)
Preview Answer:
1Msg_dic = {'Apple': 10,'Tesla': 100000,'mac': 3000,'Lenovo': 30000,'Chicken': 10,}2Shop_card = []3 whileTrue:4 forIteminchMsg_dic:5 Print('{}:{}'. Format (Item,msg_dic[item]))6 7Goods=input ('Product Name:'). Strip ()8 ifLen (goods) = =0:9 Print("No product name entered! ")Ten Continue One if notGoodsinchMsg_dic: A Print("This shop does not have this product! ") - Continue - thenum = input ('number of purchases:'). Strip () - ifLen (num) = =0: - Print("no item quantity entered! ") - Continue + if notnum.isdigit (): - Print("Product quantity format input wrong! ") + Continue A at Shop_card.append ((goods,msg_dic[goods],num)) - Print(Shop_card)
Small Knowledge Points:
In Available range:
String: substring
List: Elements
Tuples: Elements
Dictionary: Key
Python Basics (7): meta-ancestor type (Assignment supplement)