2021923学习总结
生活随笔
收集整理的這篇文章主要介紹了
2021923学习总结
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、元組
1.什么事元組(tuple)
''' 元組是容器型數據類型(序列),將()作為容器的標志里面多個元素用逗號隔開:(元素1, 元素2, 元素3,...) 元組不可變(不支持增刪改);元組事有序的(支持下標操作) 元素:任何類型的數據 '''t1 = (10, 20, 30) print(t1, type(t1)) # (10, 20, 30) <class 'tuple'>t2 = () # 空元組2.元組就是不可變的列表
列表中除了和增刪改相關操作以為的操作元組都支持
例如:查、相關操作、相關方法(除了增刪改相關的)、相關函數
t3 = (10, 23, 78, 9) print(t3[-1]) print(t3[1:])for x in t3:print(x)for index in range(len(t3)):print(index, t3[index])for index, item in enumerate(t3):print(index, item)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('是數字或者字符串')t2 = (10, 23, 78, 9, 10) print(t2.count(10)) print(t2.index(23)) print(max(t2), min(t2)) print(sum(t2)) print(sorted(t2)) # 打印結果為列表print(tuple('abc'))3.元組特殊或者常用的操作
1)只有一個元素的元組:(元素,)
t1 = (10,)2)元組在沒有歧義的情況下可以省略()
直接有逗號將多個數據隔開,表示的也是一個元組
t2 = 10, 2, 20, 3 print(t2, type(t2))3)同時使用多個變量獲取元組的元素(列表也支持)
a.讓變量的個數與元組中元素個數保持一致
t3 = ('小明', 18, 90) name, age, score = t3 print(name, age, score)b.讓變量的個數小于元組元素的個數,但是必須在某一個變量前加*
先讓不帶* 的變量按照位置獲取元素,剩下的部分全部保存到帶* 的變量對應的列表中
t3 = ('小明', 18, 170, 80, 90, 80, 76) x, y, *z = t3 print(x, y, z) # 小明 18 [170, 80, 90, 80, 76]x, *y, z = t3 print(x, z, y) # 小明 76 [18, 170, 80, 90, 80]二、字典
student = [‘張三’, 30, ‘110’, 80]
print(student[0])
student = {'name': '張四','contacts': '張三','age': 30,'tel': '110','score': 28}1.認識字典(dict)
''' 1)字典是容器型數據類型(序列),將{}作為容器的標志,里面多個鍵值對用逗號隔開:{鍵1:值1, 鍵2:值2,...} 2)字典是可變的(支持增刪改);字典是無序的(不支持下標操作) 3)元素 - 鍵值對 鍵 - 必須是不可變的數據,例如:元組、數字、字符串。鍵是唯一的 值(才是真正想保存的數據) - 沒有要求 '''1)空字典
dict1 = {} print(dict1, type(dict1))2)鍵是不可變的數據
dict2 = {'a': 10, 'b': 2} dict3 = {'b': 2, (1, 2): 10}3)鍵是唯一
dict4 = {'a': 12, '2': 44, 'a': 33} # {'a': 33, '2': 44} print(dict4)三、獲取字典的值
字典的查操作是獲取字典的值
1.查單個
''' 1)字典[鍵] - 獲取字典中指定鍵對應的值;當鍵不存在的時候會報錯 2)字典.get(鍵) - 獲取字典中指定鍵對應的值;當值不存在不會報錯,返回None 3)字典.get(鍵,默認值) - 獲取字典中指定鍵對應的值;當鍵不存在不會報錯,返回默認值 ''' dog = {'name': '小黑', 'breed': '哈士奇', 'age': 3, 'color': '黑色'} print(dog['breed']) print(dog.get('name'))student = {'name': '小明'} print(student.get('age', 18))2.遍歷
''' for 變量 in 字典:循環體變量取的是鍵 ''' dog = {'name': '小黑', 'breed': '哈士奇', 'age': 3, 'color': '黑色'} for x in dog:print(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.get('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. 計算所有學生的平均分
score1 = [] for x in class1['students']:score1.append(x['score']) print(sum(score1) / len(score1))四、字典的增刪改
1.增 - 添加鍵值對
2.改 - 修改對應的值
語法:字典[鍵] = 值 - 當鍵存在的時候是修改,鍵不存在就是添加
goods = {'name': '泡面','price': 3.5,'count': 10 } print(goods)goods['price'] = 4 print(goods)3.刪
1)del 字典[鍵] - 刪除字典中指定鍵的鍵值對
goods = {'name': '泡面','price': 3.5,'count': 10 } del goods['price'] print(goods)2)字典.pop(鍵) - 取出字典中指定鍵對應的值
goods = {'name': '泡面','price': 3.5,'count': 10 } result = goods.pop('price') print(goods)總結
以上是生活随笔為你收集整理的2021923学习总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于python下django框架 实现
- 下一篇: 基于SSH开发校园失物招领网