1. 程式人生 > >Python練習五:字典

Python練習五:字典

字典兩大特點:無序,鍵唯一

 

建立字典

1 dic1 = {'name':'CatdeXin','age':22,'bobby':'學習','Speciality':'跑步'}
2 dic2 = dict((('name','CatdeXin'),))
3 print(dic1)
4 print(dic2)

對應操作

 1 dic3 = {}
 2 dic3['name'] = 'Miao'
 3 dic3['age'] = 12
 4 print(dic3) 
 5 #{'name': 'Miao', 'age': 12}
 
 7 a = dic3.setdefault('
name','yue') 8 b = dic3.setdefault('ages',18) 9 print(a,b) 10 #yue 18 11 print(dic3) 12 #{'name': 'yue', 'ages': 18}