日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

Python第一阶段学习总结

發(fā)布時(shí)間:2024/3/12 python 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python第一阶段学习总结 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

【第7天】Python第一階段學(xué)習(xí)總結(jié)

2021/09/23

一. 元組

  • 元組的定義

    • 元組是容器型數(shù)據(jù)(序列),將()作為容器的標(biāo)志里面多個(gè)元素用逗號(hào)隔開:(元素1, 元素2,…)
    • 元組不可變(不支持增刪改);元組是有序的(支持下標(biāo)操作)
    • 元素:任何類型的數(shù)據(jù)
    t1 = (10, 20, 30) print(t1, type(t1))t2 = (10, 'abc', False, [10, 230]) print(t2)t3 = () # 空元組 print(t3, type(t3))
  • 元組就是不可變的列表

    • 列表中除了和增、刪、改相關(guān)操作以外的操作,元組都支持

    例如:查、相關(guān)操作、相關(guān)方法(除了增刪改相關(guān)的)、相關(guān)函數(shù)

    # 判斷一個(gè)數(shù)據(jù)是否是某種類型中的一種 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ù)字或者字符串')
  • 元組特殊或者常用的操作

  • 只有一個(gè)元素的元組:(元素,)

    list1 = [10] print(list1, type(list1))t1 = (10,) print(t1, type(t1))
  • 元組在沒有歧義的情況下可以省略()。直接用逗號(hào)將多個(gè)數(shù)據(jù)隔開,表示的也是一個(gè)元組

    t2 = 10, 20, 30, 40, 50 print(t2, type(t2))
  • 同時(shí)使用多個(gè)變量獲取元組的元素(列表也支持)

    t3 = ('小明', 18, 90) print(t3[0])

    a. 讓變量的個(gè)數(shù)和元組中元素個(gè)數(shù)保持一致

    name, age, score = t3 print(name, age, score)point = (100, 200) x, y = point print(x, y)

    b. 讓變量的個(gè)數(shù)小于元組元素的個(gè)數(shù),但是必須在某一個(gè)變量前加*

    ? 先讓不帶 * 的變量按照位置獲取元素,剩下的部分全部保存到帶 * 的變量對(duì)應(yīng)的列表中

    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]
  • 二. 字典

  • 什么是字典

  • 字典是容器型數(shù)據(jù)類型(序列),將{}作為容器的標(biāo)志,里面多個(gè)鍵值對(duì)用逗號(hào)隔開:{鍵1:值1,鍵2:值2, 鍵3:值3,…}
    鍵值對(duì): 鍵:值

  • 字典是可變的(支持增刪改);字典是無序的(不支持下標(biāo)操作)

  • 元素 - 鍵值對(duì)
    鍵 - 必須是不可變的數(shù)據(jù),例如:元組、數(shù)字、字符串。鍵是唯一的。
    值(才是真正想要保存的數(shù)據(jù)) - 沒有要求

  • # 1)空字典 dict1 = {} print(dict1, type(dict1))# 2) 鍵是不可變的數(shù)據(jù) dict2 ={'a': 10, 1: 20, (1, 2): 30} print(dict2)# dict2 ={'a': 10, 1: 20, [1, 2]: 30} # print(dict2) # 報(bào)錯(cuò)!# 3)鍵是唯一的 dict3 = {'a': 10, 1: 20, 'a': 30} print(dict3) # {'a': 30, 1:

    三. 獲取字典的值

  • 查單個(gè)

  • 字典[鍵] - 獲取字典中指定鍵對(duì)應(yīng)的值;當(dāng)鍵不存在的時(shí)候會(huì)報(bào)錯(cuò)
  • 字典.get(鍵) == 字典.get(鍵,None) - 獲取字典中對(duì)應(yīng)的值;當(dāng)鍵不存在不會(huì)報(bào)錯(cuò),返回None
  • 字典.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'))# print(dog['gender']) # 報(bào)錯(cuò) print(dog.get('gender')) # Nonestudent = {'name': '小明', 'age': 18, 'gender': '男'} print(student['age']) print(student.get('name')) print(student.get('contacts', '110'))
  • 遍歷

    for 變量 in 字典:循環(huán)體注意: 變量取到的是鍵dog = {'name': '小黑', 'breed': '哈士奇', 'age': 3, 'color': '黑色'} for x in dog:print(x, dog[x]) # 練習(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': '陳來','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['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é)生的平均分 scores = [] for x in class1['students']:print(x['score'])scores.append(x['score']) print('平均分是:', sum(scores) / len(scores))
  • 四. 字典的增刪改

  • 增 - 添加鍵值對(duì)

  • 改 - 修改鍵對(duì)應(yīng)的值

  • 語法:字典[] =- 當(dāng)鍵存在的時(shí)候是修改,鍵不存在是修改 goods = {'name': '泡面', 'price': 3.5} print(goods)goods['count'] = 10 print(goods)goods['price'] = 4 print(goods)
  • del 字典[鍵] - 刪除字典中指定鍵對(duì)應(yīng)的鍵值對(duì)

  • goods = {'name': '泡面', 'price': 4, 'count': 10} del goods['price'] print(goods)
  • 字典.pop(鍵) - 取出字典中指定鍵對(duì)應(yīng)值
  • goods = {'name': '泡面', 'price': 4, 'count': 10} result = goods.pop('price') print(goods) # {'name': '泡面', 'count': 10} print(result) # 4

    總結(jié)

    以上是生活随笔為你收集整理的Python第一阶段学习总结的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。