1. 程式人生 > >python基礎(第五天)

python基礎(第五天)

int 不可變 有序 clear tuple val ket def python3.6

1、數據類型:

1> 可變數據類型:list, dict -----> 不可哈希

2>不可變數據類型:int, str, bool,tuple---->可哈希

哈希算法其實是一種二分查找法,效率最高。

2、字典的操作:

 1 dic = {"name": "eric", "age": 18, "sex": "male", "salary": 18000}
 2 
 3 #
 4 # dic["hobby"] = "basketball"
 5 # print(dic)  # {‘name‘: ‘eric‘, ‘age‘: 18, ‘sex‘: ‘male‘, ‘salary‘: 18000, ‘hobby‘: ‘basketball‘}
6 # dic.setdefault("love", "girl") # 如果key存在,則返回value,如果不存在,則添加 7 # print(dic) # {‘name‘: ‘eric‘, ‘age‘: 18, ‘sex‘: ‘male‘, ‘salary‘: 18000, ‘hobby‘: ‘basketball‘, ‘love‘: ‘girl‘} 8 # print(dic.setdefault("age")) # 18 9 10 # 11 # dic.pop("sex") 12 # print(dic) 13 # dic.popitem() # 從最後一個開始刪 14
# print(dic) 15 # dic.clear() 16 17 # 18 dic["age"] = 28 19 print(dic) 20 21 # 22 # print(dic.keys()) # dict_keys([‘name‘, ‘age‘, ‘sex‘, ‘salary‘]) 23 # for i in dic.keys(): 24 # print(i) 25 26 # for i in dic.values(): 27 # print(i) 28 29 for k, v in dic.items(): 30 print(k, v) 31
""" 32 name eric 33 age 28 34 sex male 35 salary 18000 36 """

註意:字典在python3.6之前是無序,之後是有序的。

python基礎(第五天)