2021923学习总结
生活随笔
收集整理的這篇文章主要介紹了
2021923学习总结
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一、元組
1.什么事元組(tuple)
''' 元組是容器型數(shù)據(jù)類型(序列),將()作為容器的標(biāo)志里面多個(gè)元素用逗號(hào)隔開:(元素1, 元素2, 元素3,...) 元組不可變(不支持增刪改);元組事有序的(支持下標(biāo)操作) 元素:任何類型的數(shù)據(jù) '''t1 = (10, 20, 30) print(t1, type(t1)) # (10, 20, 30) <class 'tuple'>t2 = () # 空元組2.元組就是不可變的列表
列表中除了和增刪改相關(guān)操作以為的操作元組都支持
例如:查、相關(guān)操作、相關(guān)方法(除了增刪改相關(guān)的)、相關(guān)函數(shù)
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('是數(shù)字或者字符串')if t_num in (int, float, str):print('是數(shù)字或者字符串')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)) # 打印結(jié)果為列表print(tuple('abc'))3.元組特殊或者常用的操作
1)只有一個(gè)元素的元組:(元素,)
t1 = (10,)2)元組在沒(méi)有歧義的情況下可以省略()
直接有逗號(hào)將多個(gè)數(shù)據(jù)隔開,表示的也是一個(gè)元組
t2 = 10, 2, 20, 3 print(t2, type(t2))3)同時(shí)使用多個(gè)變量獲取元組的元素(列表也支持)
a.讓變量的個(gè)數(shù)與元組中元素個(gè)數(shù)保持一致
t3 = ('小明', 18, 90) name, age, score = t3 print(name, age, score)b.讓變量的個(gè)數(shù)小于元組元素的個(gè)數(shù),但是必須在某一個(gè)變量前加*
先讓不帶* 的變量按照位置獲取元素,剩下的部分全部保存到帶* 的變量對(duì)應(yīng)的列表中
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.認(rèn)識(shí)字典(dict)
''' 1)字典是容器型數(shù)據(jù)類型(序列),將{}作為容器的標(biāo)志,里面多個(gè)鍵值對(duì)用逗號(hào)隔開:{鍵1:值1, 鍵2:值2,...} 2)字典是可變的(支持增刪改);字典是無(wú)序的(不支持下標(biāo)操作) 3)元素 - 鍵值對(duì) 鍵 - 必須是不可變的數(shù)據(jù),例如:元組、數(shù)字、字符串。鍵是唯一的 值(才是真正想保存的數(shù)據(jù)) - 沒(méi)有要求 '''1)空字典
dict1 = {} print(dict1, type(dict1))2)鍵是不可變的數(shù)據(jù)
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.查單個(gè)
''' 1)字典[鍵] - 獲取字典中指定鍵對(duì)應(yīng)的值;當(dāng)鍵不存在的時(shí)候會(huì)報(bào)錯(cuò) 2)字典.get(鍵) - 獲取字典中指定鍵對(duì)應(yīng)的值;當(dāng)值不存在不會(huì)報(bào)錯(cuò),返回None 3)字典.get(鍵,默認(rèn)值) - 獲取字典中指定鍵對(duì)應(yīng)的值;當(dāng)鍵不存在不會(huì)報(bào)錯(cuò),返回默認(rèn)值 ''' dog = {'name': '小黑', 'breed': '哈士奇', 'age': 3, 'color': '黑色'} print(dog['breed']) print(dog.get('name'))student = {'name': '小明'} print(student.get('age', 18))2.遍歷
''' for 變量 in 字典:循環(huán)體變量取的是鍵 ''' dog = {'name': '小黑', 'breed': '哈士奇', 'age': 3, 'color': '黑色'} for x in dog:print(x)用一個(gè)字典保存一個(gè)班級(jí)信息
class1 = {'name': 'Python2106','address': '9教','lecturer': {'name': '余婷','age': 18,'tel': '13678192302','QQ': '726550822'},'head_teacher': {'name': '張瑞燕','tel': '110','QQ': '7283211'},'students': [{'name': '陳來(lái)','age': 20,'gender': '女','score': 98,'contacts': {'name': 'p2','tel': '120'}},{'name': '葛奕磊','age': 25,'gender': '男','score': 80,'contacts': {'name': 'p1','tel': '119'}}] }1.獲取班級(jí)的名字
print(class1.get('name'))2.獲取講師的名字和年齡
print(class1['lecturer']['name'], class1['lecturer']['age'])3.獲取班主任的名字和電話
print(class1['head_teacher']['name'], class1['head_teacher']['tel'])4. 獲取第一個(gè)學(xué)生的姓名
print(class1['students'][0]['name'])5. 獲取所有學(xué)生的聯(lián)系人的名字
for x in class1['students']:print(x['contacts']['name'])6. 計(jì)算所有學(xué)生的平均分
score1 = [] for x in class1['students']:score1.append(x['score']) print(sum(score1) / len(score1))四、字典的增刪改
1.增 - 添加鍵值對(duì)
2.改 - 修改對(duì)應(yīng)的值
語(yǔ)法:字典[鍵] = 值 - 當(dāng)鍵存在的時(shí)候是修改,鍵不存在就是添加
goods = {'name': '泡面','price': 3.5,'count': 10 } print(goods)goods['price'] = 4 print(goods)3.刪
1)del 字典[鍵] - 刪除字典中指定鍵的鍵值對(duì)
goods = {'name': '泡面','price': 3.5,'count': 10 } del goods['price'] print(goods)2)字典.pop(鍵) - 取出字典中指定鍵對(duì)應(yīng)的值
goods = {'name': '泡面','price': 3.5,'count': 10 } result = goods.pop('price') print(goods)總結(jié)
以上是生活随笔為你收集整理的2021923学习总结的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 基于python下django框架 实现
- 下一篇: 基于SSH开发校园失物招领网