python之路——6
阿新 • • 發佈:2019-03-18
lex input 練習 內存 nbsp {} flag sci class unicode A=4個字節=32位
utf-8 A=1個字節=8位
中=3個字節=24位
gbk A=1個字節=8位
中=2個字節=16位
a各個編碼之間的二進制,不能互相識別,會產生亂碼
b文件的儲存,傳輸,不能是unicode, 一般為utf-8 gbk等 中文:
str : 表現形式 : s= ‘中國‘
編碼方式: unicode
bytes:表現形式 : s= b‘x\e91\e91\e01\e21\e31\e32‘
編碼方式: utf-8 等
復習
增
dic[‘age‘] = 21
dic.setfault()
刪
pop
clear
del
popitem()
改
update
查
dic.key()value()item()
for k,v in dic.item():
print(k,v)
dic.get(key,None)
學習內容
1. =賦值 ==比較值 is 比較內存地址
2. id() 內存地址
3. 數字,字符串 小數據池
數字的範圍: -5 -256 則共用一個內存地址
字符串:1.含有特殊字符,則不共用 2.s*20 為同一地址,s*21以後都是兩個地址
剩下的 list dic set tuple 都是不同地址
4. 編碼 ASCII碼 A=一個字節=8位
utf-8 A=1個字節=8位
中=3個字節=24位
gbk A=1個字節=8位
中=2個字節=16位
a各個編碼之間的二進制,不能互相識別,會產生亂碼
b文件的儲存,傳輸,不能是unicode, 一般為utf-8 gbk等
python3中,str 在內存在使用unicode編碼
bytes類型
英文:
str : 表現形式 : s= ‘alex‘
編碼方式: unicode
bytes:表現形式 : s= b‘alex‘
編碼方式: utf-8 等
str : 表現形式 : s= ‘中國‘
編碼方式: unicode
bytes:表現形式 : s= b‘x\e91\e91\e01\e21\e31\e32‘
編碼方式: utf-8 等
代碼區
1.字典操作練習
dic1 = {‘k1‘: ‘v1‘, ‘k2‘: ‘v2‘, ‘k3‘:[11,22,33]} for i in dic1.keys(): print(i) for i in dic1.values(): print(i) for i in dic1.items():print(i) dic1[‘k4‘] = ‘v4‘ dic1[‘k1‘] = ‘alex‘ dic1[‘k3‘].append(44) dic1[‘k3‘].insert(0 , 18) print(dic1)
2.分別保存<>66的元素到字典
li = [11, 22, 33, 44, 55, 66, 77, 88, 99, 90] key = {‘k1‘: [], ‘k2‘: []} for i in li: if i > 66: key[‘k1‘].append(i) elif i < 66: key[‘k2‘].append(i) print(key)
3.輸出打印產品列表,用戶選擇,按q 退出
prod = {1: ‘手機‘, 2: ‘電腦‘, 3: ‘鼠標墊‘, 4: ‘遊艇‘} for k, v in prod.items(): print(‘{}\t\t{}‘.format(k, v)) while 1: num_choice = input(‘請輸入商品序號‘) if num_choice.upper() == ‘Q‘: break elif num_choice.isdigit(): num_choice = int(num_choice) print(prod.get(num_choice, "查無此貨")) else: print("請輸入正確的商品序號")
4.購物車
print(‘全場十元,歡迎光臨‘) prod = {1: {‘name‘: ‘手機‘, ‘price‘: 10}, 2: {‘name‘: ‘錢包‘, ‘price‘: 10}, 3: {‘name‘: ‘鑰匙‘, ‘price‘: 10}} for k, v in prod.items(): print(‘序號:{}\t商品:{}\t價格:{}‘.format(k, v[‘name‘], v[‘price‘])) money = int(input(‘您的余額為:‘)) money1 = money flag = 1 while flag: if int(money) > 0: num_choice = input(‘請輸入商品序號‘) if num_choice.upper() == ‘Q‘: q = input(‘確認放棄本次購物請按Y,返回請按N‘) if q.upper() == ‘Y‘: flag = 0 continue else: continue elif num_choice.isdigit(): num_choice = int(num_choice) if num_choice > 0 and num_choice <= len(prod): num_buy = input(‘請輸入購買個數‘) if num_buy.isdigit(): num_buy = int(num_buy) if money >= prod[num_choice][‘price‘] * num_buy: money = money - prod[num_choice][‘price‘] * num_buy print(‘您已購買{}{}個,余額為:{}‘.format(prod[num_choice][‘name‘], num_buy, money)) else: print(‘余額不足,余額為:{}‘.format(money)) else: print("請輸入正確個數") else: print("請輸入正確的商品序號") else: print("請輸入正確的商品序號") else: print(‘您的余額不足請及時充費‘) flag = 0 print("此次共消費{},剩余{}".format(money1-money, money))
python之路——6