9月23日学习总结
9月23日學習總結
一、元組
1.元組的定義
1.元組是容器型數據(序列),將()作為容器的標志里面多個元素用逗號隔開:(元素1,元素2,…)
2.元組不可變(不支持增刪改);元組是有序的(支持下標操作)
3.元素:任何類型的數據
t1 = (10, 20, 30) print(t1, type(t1))t2 = (10, 'abc', False, [10, 230]) print(t2)t3 = () # 空元組 print(t3, type(t3))# (10, 20, 30) <class 'tuple'> # (10, 'abc', False, [10, 230]) # () <class 'tuple'>2.元組就是不可變的列表
1.列表中除了和增刪改相關操作意外的操作,元素都支持
例如:查、相關操作、相關方法(除了增刪改相關的)、相關函數
# 判斷一個數據是否是某種類型中的一種 num = 23 t_num = type(num) if t_num == int or t_num == float or t_num == str:print('是數字或者字符串')if t_num in (int, float, str):print('是數字或者字符串')3.元組特殊或者常用的操作
1.只有一個元素的元組:(元素,)
list1 = [10] print(list1, type(list1))t1 = (10,) print(t1, type(t1)) # [10] <class 'list'> # (10,) <class 'tuple'>2.元組在沒有歧義的情況下可以省略()。直接用逗號將多個數據隔開,表示的也是一個元組
t2 = 10, 20, 30, 40, 50 print(t2, type(t2)) # (10, 20, 30, 40, 50) <class 'tuple'>3.同時使用多個變量獲取元組的元素(列表也支持)
t3 = ('小明', 18, 90) print(t3[0])a. 讓變量的個數和元組中元素個數保持一致
t3 = ('小明', 18, 90) name, age, score = t3 print(name, age, score)point = (100, 200) x, y = point print(x, y)b. 讓變量的個數小于元組元素的個數,但是必須在某一個變量前加*
先讓不代 * 的變量按照位置獲取元素,剩下部分全部保存到帶 * 的變量對應的列表中
t3 = ('小明', 18, 170, 80, 99, 80, 76) x, y, *z = t3 print(x, y, z) # 小明 18 [170, 80, 99, 80, 76]x, *y, z = t3 # 小明 [18, 170, 80, 99, 80] 76 print(x, y, z)*x, y, z = t3 # ['小明', 18, 170, 80, 99] 80 76 print(x, y, z)x, *y, z, t = t3 print(z) # 80 print(y) # [18, 170, 80, 99]二、字典
1.什么是字典
字典是容器型數據類型(序列),將{}作為容器的標志,里面多個鍵值對用逗號隔開:{鍵1:值1,鍵2:值2,鍵3:值3,…}
鍵值對:鍵:值
2.字典是可變的(支持增刪改);字典是無序的(不支持下標操作)
3.元素 - 鍵值對
鍵 - 必須是不可變的數據,例如:元組,數字,字符串。鍵是唯一的。
值(才是真正想要保存的數據) - 沒有要求
# 1)空字典 dict1 = {} print(dict1, type(dict1))# 2) 鍵是不可變的數據 dict2 ={'a': 10, 1: 20, (1, 2): 30} print(dict2)# dict2 ={'a': 10, 1: 20, [1, 2]: 30} # print(dict2) # 報錯!# 3)鍵是唯一的 dict3 = {'a': 10, 1: 20, 'a': 30} print(dict3) # {'a': 30, 1: 20}三、獲取字典的值
1.查單個
1.字典[鍵] - 獲取字典中指定鍵對應的值,當鍵不存在的時候會報錯
2.字典.get(鍵)==字典.get(鍵,None) - 獲取字典中對應的值;當鍵不存在不會報錯,返回None
3.字典.get(鍵,默認值) - 獲取字典中對應的值;當鍵不存在不會報錯,返回默認值
dog = {'name': '小黑', 'breed': '哈士奇', 'age': 3, 'color': '黑色'} print(dog['breed']) print(dog.get('name'))# print(dog['gender']) # 報錯 print(dog.get('gender')) # Nonestudent = {'name': '小明', 'age': 18, 'gender': '男'} print(student['age']) print(student.get('name')) print(student.get('contacts', '110'))# 哈士奇 # 小黑 # None # 18 # 小明 # 1102.遍歷
# for 變量 in 字典: # 循環體 # # 注意: 變量取到的是鍵dog = {'name': '小黑', 'breed': '哈士奇', 'age': 3, 'color': '黑色'} for x in dog:print(x, dog[x]) # 練習:用一個字典保存一個班級信息 class1 = {'name': 'Python2106','address': '9教','lecturer': {'name': '余婷','age': 18,'tel': '13678192302','QQ': '726550822'},'head_teacher': {'name': '張瑞燕','tel': '110','QQ': '7283211'},'students': [{'name': '陳來','age': 20,'gender': '女','score': 98,'contacts': {'name': 'p2','tel': '120'}},{'name': '葛奕磊','age': 25,'gender': '男','score': 80,'contacts': {'name': 'p1','tel': '119'}}] }# 1. 獲取班級的名字 print(class1['name'])# 2. 獲取講師的名字和年齡 print(class1['lecturer']['name'], class1['lecturer']['age'])# 3. 獲取班主任的名字和電話 print(class1['head_teacher']['name'], class1['head_teacher']['tel'])# 4. 獲取第一個學生的姓名 print(class1['students'][0]['name'])# 5. 獲取所有學生的聯系人的名字 for x in class1['students']:print(x['contacts']['name'])# 6. 計算所有學生的平均分 scores = [] for x in class1['students']:print(x['score'])scores.append(x['score']) print('平均分是:', sum(scores) / len(scores))四、字典的增刪改
1.增 - 添加鍵值對
2.改 - 修改鍵對應的值
語法:字典[鍵] = 值 - 當鍵存在的時候是修改,鍵不存在就是增加
goods = {'name': '泡面', 'price': 3.5} print(goods)goods['count'] = 10 print(goods)goods['price'] = 4 print(goods)# {'name': '泡面', 'price': 3.5} # {'name': '泡面', 'price': 3.5, 'count': 10} # {'name': '泡面', 'price': 4, 'count': 10}3.刪
(1)del 字典[鍵] - 刪除字典中指定鍵對應的鍵值對
goods = {'name': '泡面', 'price': 4, 'count': 10} del goods['price'] print(goods) # {'name': '泡面', 'count': 10}(2)字典.pop(鍵) - 取出字典中指定鍵對應值
goods = {'name': '泡面', 'price': 4, 'count': 10} result = goods.pop('price') print(goods) # {'name': '泡面', 'count': 10} print(result) # 4 # {'name': '泡面', 'count': 10} # 4總結
- 上一篇: oracle exp导出很慢,oracl
- 下一篇: IPCamera开源项目