国产日韩欧美一区二区三区综合,日本黄色免费在线,国产精品麻豆欧美日韩ww,色综合狠狠操

極客小將

您現(xiàn)在的位置是:首頁 » python編程資訊

資訊內(nèi)容

Python最詳細(xì)之?dāng)?shù)據(jù)類型講解

極客小將2021-01-11-
簡介Python教程欄目介紹相關(guān)數(shù)據(jù)類型相關(guān)免費(fèi)學(xué)習(xí)推薦:python教程(視頻)字符串字符串的坑:三引號的字符串如果中間沒有雙引號的字符串,會在解釋器中輸出為雙引號三引號的字符串如果中間有雙引號的字符串,會在解釋器中輸出為單引號字符串是存儲在內(nèi)存中字符串的操作:輸入:input()輸出:print()

極客小將版權(quán)所有。

python教程欄目介紹相關(guān)數(shù)據(jù)類型

dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

相關(guān)免費(fèi)學(xué)習(xí)推薦:python教程(視頻)dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

字符串dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

字符串的坑:dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

三引號的字符串如果中間沒有雙引號的字符串,會在解釋器中輸出為雙引號dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

三引號的字符串如果中間有雙引號的字符串,會在解釋器中輸出為單引號dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

字符串是存儲在內(nèi)存中dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

字符串的操作:dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

輸入:input()dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

輸出:print()dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

索引:str[index]dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

取地址: id(str)dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

切片:str[start: end: step] (負(fù)數(shù)步長 -- 倒序選取; 選取方向需一致,否則無法選取數(shù)據(jù))dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

查找:find():語法:str.find(substr, start, end)注意:返回值是第一次出現(xiàn)的索引值;如果子串不存在返回-1,不會報(bào)錯.dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

例如:mystr = 'hello world and itcast and itheima and Python' print(mystr.find('and')) # 12 print(mystr.find('and',15,30)) # 23 print(mystr.find('ands')) # -1 ands子串不存在 index():dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

語法: str.index(substr, start, end)返回值是第一次出現(xiàn)的索引值;如果子串不存在,會報(bào)錯例如:mystr = 'hello world and itcast and itheima and Python'print(mystr.index('and')) # 12 print(mystr.index('and',15,30)) # 23print(mystr.index('ands')) #如果index查找子串不存在,報(bào)錯 count(): 統(tǒng)計(jì)子串出現(xiàn)的次數(shù)語法: str.index(substr, tart, end)注意: 返回值是子串出現(xiàn)的次數(shù),如果子串不存在返回0,不會報(bào)錯例如:mystr = 'hello world and itcast and itheima and Python' print(mystr.count('and',15,30)) # 1 print(mystr.count('and')) # 3print(mystr.count('ands')) # 0 rfind(): 和find()功能相同,但查找方向?yàn)橛覀?cè)開始rindex(): 和index()功能相同,但查找訪問為右側(cè)開始dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

修改:replace() : 替換語法: str.replace(舊子串,新子串, 替換次數(shù))注意:如果不寫替換次數(shù),默認(rèn)會將舊子串全部替換替換次數(shù)如果超出子串出現(xiàn)次數(shù),則替換次數(shù)為該子串出現(xiàn)的次數(shù)。調(diào)用replace函數(shù)后,發(fā)現(xiàn)原有字符串的數(shù)據(jù)并沒有做到修改,修改后的數(shù)據(jù)是replace函數(shù)的返回值 說明: 字符串是不可變數(shù)據(jù)類型例如: mystr = 'hello world and itcast and itheima and Python' new_str = mystr.replace('and','he') new_str = mystr.replace('and','he',1) new_str = mystr.replace('and','he',10) split() : 分割語法:str.split(分割字符, num)注意:返回值是一個列表, 丟失分割字符num 表示的是分隔字符出現(xiàn)的次數(shù),即將來返回?cái)?shù)據(jù)個數(shù)為num+1個dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

例如mystr = 'hello world and itcast and itheima and Python' list1 = mystr.split('and') list1 = mystr.split('and',2) join():dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

合并語法:字符或子串. join(多字符組成的序列)注意:用一個子串或子串合并字符串,即是將多個字符串合并為一個新的字符串例如:mylist = ['aa','bb','cc'] new_str = '...'.join(mylist) print(new_str)dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

大小寫轉(zhuǎn)換capitalize() : 將字符串第一個字符轉(zhuǎn)換成大寫語法:str.capitalize()注意:capitalize()函數(shù)轉(zhuǎn)換后,只字符串第一個字符大寫,其他的字符全都小寫dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

例如:mystr = 'hello world and itcast and itheima and Python' new_str = mystr.capitalize() print(new_str) title(): 將字符串每個單詞首字母轉(zhuǎn)換成大寫語法: str.title()dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

例如:mystr = 'hello world and itcast and itheima and Python' new_str = mystr.title() print(new_str) lower(): 將字符串中大寫轉(zhuǎn)小寫語法: str.lower()注意:全部轉(zhuǎn)為小寫dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

例如: mystr = 'hello world and itcast and itheima and Python'new_str = mystr.lower() print(new_str) upper(): 將字符串中小寫轉(zhuǎn)大寫語法: str.upper()注意:全部轉(zhuǎn)為小寫dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

例如:mystr = 'hello world and itcast and itheima and Python' new_str = mystr.upper() print(new_str)dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

刪除前后空格lstrip(): 刪除字符串左側(cè)空白字符語法: str.lstrip()例如:mystr = ' hello world and itcast and itheima and Python ' new_str = mystr.lstrip() print(new_str) rstrip(): 刪除字符串右側(cè)空白字符語法: str.rstrip()dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

例如:mystr = ' hello world and itcast and itheima and Python ' new_str = mystr.rstrip() print(new_str) strip(): 刪除字符串兩側(cè)側(cè)空白字符語法: str.strip()dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

例如:mystr = ' hello world and itcast and itheima and Python ' new_str = mystr.strip() print(new_str)dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

字符串對齊ljust(): 返回一個原字符串左對齊,并使用指定字符(默認(rèn)空格)填充至對應(yīng)長度的新字符串語法: str.ljust(長度,填充字符)例如:mystr = "hello"print(mystr.ljust(10,'.')) 效果為: 'hello.....' rjust(): 返回一個原字符串右對齊,并使用指定字符(默認(rèn)空格)填充至對應(yīng)長度的新字符串語法: str.rjust(長度,填充字符) dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

例如:mystr = "hello" print(mystr.rjust(10,'.')) 效果為: '.....hello' center():返回一個原字符串居中對齊,并使用指定字符(默認(rèn)空格)填充至對應(yīng)長度的新字符串語法: str.center(長度,填充字符)例如: mystr = "hello" print(mystr.conter(10,'.')) 效果為: '..hello...'dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

判斷開頭結(jié)尾startswith(): 檢查字符串是否是以指定子串開頭,是則返回True,否則返回False。如果設(shè)置開始和結(jié)束位置下標(biāo),則在指定范圍內(nèi)檢查語法 : str.startswith(子串,start,end)dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

例如: mystr = 'hello world and itcast and itheima and Python' print(mystr.startswith('hello')) print(mystr.startswith('hel')) print(mystr.startswith('hels')) endswith(): 檢查字符串是否是以指定子串結(jié)尾,是則返回True,否則返回False。如果設(shè)置開始和結(jié)束位置下標(biāo),則在指定范圍內(nèi)檢查語法:str.endswith(子串,start,end)dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

例如:mystr = 'hello world and itcast and itheima and Python' print(mystr.endswith('Python')) print(mystr.endswith('Pythons'))dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

判斷isalpha(): 如果字符串至少有一個字符并且所有字符都是字母則返回True,否則返回False語法: str.isalpha()例如: mystr = 'hello world and itcast and itheima and Python' print(mystr.isalpha()) isdigit(): 如果字符串只包含數(shù)字則返回True,否則返回Flase.語法: str.isdigit()dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

例如:mystr1 = '12345' print(mystr1.isdigit()) isalnum(): 如果字符串至少有一個字符并且所有字符都是字母或數(shù)字語法: str.lsalnum()dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

例如:mystr2 = 'abc123' print(mystr2.isalnum()) isspace(): 如果字符串中只包含空白,則返回True,否則返回False.語法: str.isspace()dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

例如:mystr3 = ' 'print(mystr3.isspace())dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

列表dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

列表的坑:dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

列表是數(shù)據(jù)結(jié)構(gòu)的一種具體的體現(xiàn)dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

列表的格式[數(shù)據(jù)1, 數(shù)據(jù)2, 數(shù)據(jù)3]dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

在工作中,列表盡可能的存儲相同類型的數(shù)據(jù)dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

列表的操作:dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

增append(): 列表結(jié)尾追加數(shù)據(jù)語法: list.append(數(shù)據(jù))注意:列表追加數(shù)據(jù)的時候,直接在原列表里面追加了指定數(shù)據(jù),即修改了原列表,故列表為可變類型數(shù)據(jù)。如果append()追加的數(shù)據(jù)是一個序列,則追加整個序列到列表dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

例如:name_list = ['Tom','Lily','Rost'] name_list.append('xiaoming') print(name_list) # 結(jié)果: ['Tom','Lily','Rost','xiaoming'] extend(): 列表結(jié)尾追加數(shù)據(jù),如果數(shù)據(jù)是一個序列,則將這個序列的數(shù)據(jù)逐一添加到列表語法: list.extend(數(shù)據(jù))dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

例如:name_list = ['Tom','Lily','Rost'] name_list.extend('xiaoming') print(name_list) # 結(jié)果: ['Tom','Lily','Rost','x','i','a',...] insert(): 指定位置新增數(shù)據(jù)語法: list.insert(位置下標(biāo),數(shù)據(jù))例如:name_list = ['Tom','Lily','Rost'] name_list.insert(1, 'xiaoming') print(name_list) # 結(jié)果: ['Tom','xiaoming','Lily','Rost']dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

判斷是否存在in:語法: str in listdIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

例如:name_list = ['Tom','Lily','Rost'] print('Lily' inname_list) # True print('Lilys' in name_list) # Flase not in:語法: str not in list例如:name_list = ['Tom','Lily','Rost'] print('Lily' not in name_list) # Flaseprint('Lilys' not in name_list) # TruedIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

刪del:語法: del 目標(biāo)例如:name_list = ['Tom','Lily','Rost'] del name_list[0] print(name_list) # ['Lily','Rost'] pop(): 刪除指定下標(biāo)的數(shù)據(jù)(默認(rèn)為**后一個),并返回該數(shù)據(jù) 無論是按照下標(biāo)還是刪除**后一個, pop函數(shù)會返回這個被刪除的數(shù)據(jù)語法: list.pop()dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

例如:name_list = ['Tom','Lily','Rost'] del_name = name_list.pop(1) print(del_name) # 'Lily' print(name_list) # ['Tom','Rost']dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

remove(): 移除列表中某個數(shù)據(jù)的第一個匹配項(xiàng)語法: list.remove(數(shù)據(jù))dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

例如: name_list = ['Tom','Lily','Rose'] name_list.remove('Rose') print(name_list) # ['Tom','Lily'] clear(): 清空列表語法: list.clear()例如: name_list = ['Tom','Lily','Rost'] name_list.clear() print(name_list) # []dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

改修改指定下標(biāo)數(shù)據(jù) dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

例如:name_list = ['Tom','Lily','Rost'] name_list[0] = 'aaa'print(name_list) # ['aaa','Lily','Rost'] reverse() :逆置語法: list.reverse()dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

例如:num_list = [1,5,2,3,6,8] num_list.reverse() print(num_list) # [8,6,3,2,5,1]sort(): 排序語法: list.sort(key=None,reverse=False)注意: reverse表示排序規(guī)則, reverse = True 降序, reverse = False 升序(默認(rèn))dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

例如:num_list = [1,5,2,3,6,8] num_list.sort() print(num_list) # [1,2,3,5,6,8]dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

查下標(biāo):index(): 返回指定數(shù)據(jù)所在位置的下標(biāo) -- 如果查找的數(shù)據(jù)不存在則報(bào)錯語法: list.index(數(shù)據(jù),start,end)例如:name_list = ['Tom','Lily','Rose'] print(name_list.index('Lily',0,2)) # 1 count(): 統(tǒng)計(jì)指定數(shù)據(jù)在當(dāng)前列表中持續(xù)的次數(shù)語法: list.count()dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

例如:name_list = ['Tom','Lily','Rose'] print(name_list.count('Lily')) len(): 訪問列表長度,即列表中數(shù)據(jù)的個數(shù)語法: len(list)dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

例如:name_list = ['Tom','Lily','Rose'] print(len(name_list)) # 3dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

復(fù)制copy(): 開辟新空間存儲數(shù)據(jù)語法: list.copy()例如:name_list = ['Tom','Lily','Rose'] name_li2 = name_list.copy() print(name_li2) # ['Tom','Lily','Rose']dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

遍歷while: 依次打印列表中的各個數(shù)據(jù)例如:name_list = ['Tom','Lily','Rose'] i = 0 while i < len(name_list): print(name_list[i]) i +=1 # 結(jié)果:Tom Lily Rose for: 依次打印列表中的各個數(shù)據(jù)例如:name_list = ['Tom','Lily','Rose'] for i in name_list: print(i) # 結(jié)果:Tom Lily RosedIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

列表嵌套概念: 列表嵌套指的就是一個列表里面包含了其他的子列表dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

例如:name_list = [['小明','小紅','小綠'],['Tom','Lily','Rose'],['張三','李四','王二']] print(name_list[2]) # ['張三','李四','王二'] print(name_list[2][1]) # 李四dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

元組dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

元組的坑:dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

一個元組可以存儲多個數(shù)據(jù), 元組內(nèi)的數(shù)據(jù)是不能修改的dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

元組的格式: (數(shù)據(jù)1, 數(shù)據(jù)2, 數(shù)據(jù)3)dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

如果定義的元組只有一個數(shù)據(jù),那個這個數(shù)據(jù)后面也**好添加逗號, 否則數(shù)據(jù)類型為**的這個數(shù)據(jù)的數(shù)據(jù)類型dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

在工作中,元組盡可能的存儲相同類型的數(shù)據(jù)dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

元組的操作:dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

查找按下標(biāo)查找數(shù)據(jù):例如:tuple1 = ('aa','bb','cc','bb') print(tuple1[0]) # aaindex(): 查找某個數(shù)據(jù),如果數(shù)據(jù)存在返回對應(yīng)的下標(biāo),否則報(bào)錯語法: 語法和列表、字符串的index方法相同 -- tuple.index(子串)例如:tuple1 = ('aa','bb','cc','bb') print(tuple1.index('aa')) # 0 count(): 統(tǒng)計(jì)某個數(shù)據(jù)在當(dāng)前元組出現(xiàn)的次數(shù)語法: tuple.count(數(shù)據(jù))例如:tuple1 = ('aa','bb','cc','bb') print(tuple1.count('bb')) # 1len() : 統(tǒng)計(jì)元組中數(shù)據(jù)的個數(shù)語法: len(tuple)例如:tuple1 = ('aa','bb','cc','bb') print(len(tuple1)) # 4dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

修改元組內(nèi)的直接數(shù)據(jù)如果修改則立即報(bào)錯例如:tuple1 = ('aa','bb','cc','bb') tuple1[0] = 'aaa' # 報(bào)錯 如果元組里面有列表,修改列表里面的數(shù)據(jù)例如:tuple2 = (10, 20, ['aa', 'bb', 'cc'], 50, 30) print(tuple2[2]) # 訪問到列表 tuple2[2][0] = 'aaaaa' print(tuple2) # (10, 20, ['aaaaa', 'bb', 'cc'], 50, 30)dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

字典dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

字典的坑:dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

字典里面的數(shù)據(jù)是以鍵值對形式出現(xiàn),字典數(shù)據(jù)和數(shù)據(jù)順序沒有關(guān)系,即字典不支持下標(biāo),后期無論數(shù)據(jù)如何變化,只需要按照對應(yīng)的鍵的名字查找數(shù)據(jù)即可dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

字典的格式: {'key1':value1, 'key2':value2}dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

字典的操作:dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

增dict[key] = value:語法: 字典系列[key] = 值注意: 如果key存在則修改這個key對應(yīng)的值,如果key不存在則新增此鍵值對。字典為可變類型例如:dict1 = {'name':'Tom', 'age':20; 'gender':'男'} dict1['name'] = 'Rose' print(dict1) # {'name':'Rose', 'age':20; 'gender':'男'} dict1['id'] = 110 print(dict1) # {'name':'Tom', 'age':20; 'gender':'男', 'id':110} dict(zip(list1,list2)):語法: dict(zip(list1,list2))注意:兩個列表的數(shù)據(jù)個數(shù)需要一樣例如:a = [1,2,3,4] b = [1,2,3,4] c = dict(zip(a,b)) print(c) # {1:1, 2:2, 3:3, 4:4} dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

刪del()/ del: 刪除字典或刪除字典中指定鍵值對語法: del dict例如:dict1 = {'name':'Tom', 'age':20, 'gender':'男'} del dict1['gender'] print(dict1) # {'name':'Tom', 'age':20} clear(): 清空字典語法:dict.clear()例如:dict1 = {'name':'Tom', 'age':20, 'gender':'男'} dict1.clear() print(dict1) # {}dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

改修改:語法: 字典序列[key] = 值注意: 如果key存在則修改這個key對應(yīng)的值,如果key不存在就新增此鍵值對例如:dict1 = {'name':'Tom', 'age':20; 'gender':'男'} dict1['name'] = 'Rose' print(dict1) # {'name':'Rose', 'age':20; 'gender':'男'}dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

查key查找語法: dict[key]注意: 如果當(dāng)前查找的key存在,則返回對應(yīng)的值;否則報(bào)錯例如:dict1 = {'name':'Tom', 'age':20; 'gender':'男'} print(dict1['name']) # Tomprint(dict1['id']) # 報(bào)錯 get()語法: 字典序列.get(key, 默認(rèn)值)注意: 如果當(dāng)前查找的key不存在則返回第二個參數(shù)(默認(rèn)值),如果省略第二個參數(shù),則返回None例如:dict1 = {'name':'Tom', 'age':20; 'gender':'男'} print(dict1.get('name')) # Tomprint(dict1.get('id',100)) # 100 print(dict1.get('id')) # None keys()語法: dict.keys()例如:dict1 = {'name':'Tom', 'age':20; 'gender':'男'} print(dict1.keys()) # dict_keys(['name','age','gender']) values()語法: dict.values()例如:dict1 = {'name':'Tom', 'age':20; 'gender':'男'} print(dict1.values()) # dict1.values(['Tom',20,'']) items()語法: dict1.items()例如:dict1 = {'name':'Tom', 'age':20; 'gender':'男'} print(dict1.items()) # dict_items([('name','Tom'),('age',20),('gender','男')])dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

遍歷遍歷字典的key例如dict1 = {'name':'Tom', 'age':20; 'gender':'男'} for key indict1.keys(): print(key) # 結(jié)果: name age gender 遍歷字典的values例如:dict1 = {'name':'Tom', 'age':20; 'gender':'男'} for key in dict1.values(): print(key) # 結(jié)果: Tom 20 男 遍歷字典的元素例如:dict1 = {'name':'Tom', 'age':20; 'gender':'男'} for key in dict1.items(): print(key) # 結(jié)果: ('name','Tom') ('age',20) ('gender','男') 遍歷字典的鍵值對(拆包)例如:dict1 = {'name':'Tom', 'age':20; 'gender':'男'} for key,values in dict1.items(): print(f'{key} = {values}') # 結(jié)果: name = Tom age = 20 gender = 男dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

集合dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

集合的坑:dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

創(chuàng)建集合使用{}或set(),但是如果要創(chuàng)建空集合只能使用set(),因?yàn)槭褂脅}創(chuàng)建的是字典dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

集合不支持下標(biāo)操作,因?yàn)榧蠜]有順序dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

集合數(shù)據(jù)有去重功能dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

集合的操作:dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

增加數(shù)據(jù)add()語法: set.add()注意:因?yàn)榧嫌腥ブ毓δ?所以,當(dāng)向集合內(nèi)追加數(shù)據(jù)是當(dāng)前集合已有數(shù)據(jù)的話, 則不進(jìn)行任何操作例如:s1 = {10,20} s1.add(100) s1.add(10) print(s1) # {100, 10, 20} update():語法: set.update(list)注意: 追加的數(shù)據(jù)是序列例如:s1 = {10, 20} # s1.update(100) # 報(bào)錯 s1.update([100, 200]) s1.update('abc') print(s1) # {'c', 100, 'a', 200, 10, 20, 'b'}dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

刪除數(shù)據(jù)remove()語法: set.remove()注意: 刪除集合中的指定數(shù)據(jù),如果數(shù)據(jù)不存在則報(bào)錯例如:s1 = {10, 20} s1.remove(10) print(s1) # {20} s1.remove(10) print(s1) # 報(bào)錯 discard()語法: set.discard()注意: 刪除集合中的指定數(shù)據(jù), 如果數(shù)據(jù)不存在也不會報(bào)錯例如:s1 = {10, 20} s1.discard(10) print(s1) s1.discard(10) print(s1) pop()語法: set.pop()注意: 隨機(jī)刪除集合中的某個數(shù)據(jù),并返回這個數(shù)據(jù)例如:s1 = {10, 20, 30, 40 ,50} del_num = s1.pop() print(del_num) print(s1)dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

查找數(shù)據(jù)in : 判斷數(shù)據(jù)在集合序列not in :判斷數(shù)據(jù)不在集合序列例如:s1 = {10, 20, 30, 40, 50} print(10 in s1) print(10 not in s1)dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

公共操作 dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

運(yùn)算符dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

運(yùn)算符描述支持的容器類型+合并字符串、列表、元組*復(fù)制字符串、列表、元組in元素是否存在字符串、列表、元組、字典、集合not in元素是否不存在字符串、列表、元組、字典、集合dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

例如:dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

# +# 1. 字符串str1 ='aa'str2 ='bb'str3 = str1 + str2print(str3)# aabb# 2. 列表list1 = [1, 2]list2 = [10, 20]list3 = list1 + list2print(list3)# [1, 2, 10, 20]# 3. 元組t1 = (1, 2)t2 = (10, 20)t3 = t1 + t2print(t3)# (10, 20, 100, 200)# * # 1. 字符串print('-'* 10)# ----------# 2. 列表list1 = ['hello']print(list1 * 4)# ['hello', 'hello', 'hello', 'hello']# 3. 元組t1 = ('world',)print(t1 * 4)# ('world', 'world', 'world', 'world')# in 或 not in # 1. 字符串print('a'in'abcd')# Trueprint('a'notin'abcd')# False# 2. 列表list1 = ['a','b','c','d']print('a'inlist1)# Trueprint('a'notinlist1)# False# 3. 元組t1 = ('a','b','c','d')print('aa'int1)# Falseprint('aa'notint1)# TruedIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

公共方法dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

函數(shù)描述len()計(jì)算容器中元素個數(shù)del 或 del()刪除max()返回容器中元素**?值min()返回容器中元素**?值range(start, end, step)?成從start到end的數(shù)字,步?為 step,供for循環(huán)使?enumerate()函數(shù)?于將?個可遍歷的數(shù)據(jù)對象(如列表、元組或字符串)組合為?個索引序 列,同時列出數(shù)據(jù)和數(shù)據(jù)下標(biāo),?般?在 for 循環(huán)當(dāng)中dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

例如:dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

# len()# 1. 字符串str1 ='abcdefg'print(len(str1))# 7# 2. 列表list1 = [10,20,30,40]print(len(list1))# 4# 3. 元組t1 = (10,20,30,40,50)print(len(t1))# 5# 4. 集合s1 = {10,20,30}print(len(s1))# 3# 5. 字典dict1 = {'name':'Rose','age':18}print(len(dict1))# 2# del()# 1. 字符串str1 ='abcdefg'delstr1print(str1)# 2. 列表list1 = [10,20,30,40]del(list1[0])print(list1)# [20, 30, 40]# max()# 1. 字符串str1 ='abcdefg'print(max(str1))# g# 2. 列表list1 = [10,20,30,40]print(max(list1))# 40# min()# 1. 字符串str1 ='abcdefg'print(min(str1))# a# 2. 列表list1 = [10,20,30,40]print(min(list1))# 10# range() -- range()生成的序列不包含end數(shù)字# 1 2 3 4 5 6 7 8 9foriinrange(1,10,1): print(i)# 1 3 5 7 9foriinrange(1,10,2): print(i)# 0 1 2 3 4 5 6 7 8 9foriinrange(10): print(i)# enumerate() -- enumerate(可遍歷的對象, start=0)list1 = ['a','b','c','d','e']foriinenumerate(list1): print(i)forindex, charinenumerate(list1, start=1): print(f'下標(biāo)是{index}, 對應(yīng)的字符是{char}')dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

容器類型的轉(zhuǎn)換dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

tuple()作用: 將某個序列轉(zhuǎn)換成元組例如list1 = [10, 20, 30, 40, 50, 20] s1 = {100, 200, 300, 400, 500} print(tuple(list1)) print(tuple(s1))dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

list()作用: 將某個序列轉(zhuǎn)換成列表例如:t1 = ('a', 'b', 'c', 'd', 'e') s1 = {100, 200, 300, 400, 500} print(list(t1)) print(list(s1))dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

set()作用: 將某個序列轉(zhuǎn)換成集合例如:list1 = [10, 20, 30, 40, 50, 20] t1 = ('a', 'b', 'c', 'd', 'e') print(set(list1)) print(set(t1)) # 1. 集合可以快速的完成列表去重 # 2. 集合不支持下標(biāo)dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

推導(dǎo)式 -- 生成式dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

列表推導(dǎo)式作用:用一個表達(dá)式創(chuàng)建一個有規(guī)律的列表或控制一個有規(guī)律列表例如:# while 循環(huán)實(shí)現(xiàn) # 1. 準(zhǔn)備一個空列表 list1 = [] # 書寫循環(huán),依次追加數(shù)字到空列表list1中 i = 0 while i < 10: list1.append(i) i += 1 print(list1) # for 循環(huán)實(shí)現(xiàn) list1 = [] for i in range(10): list1.append(i) print(list1) # 列表推導(dǎo)式實(shí)現(xiàn) list1 = [i for i in range(10)] print(list1) # 帶if的列表推導(dǎo)式 # 方法一: range()步長實(shí)現(xiàn) list1 = [i for i in range(0,10,2)] print(list1) # 方法二: 帶if的列表推導(dǎo)式 list1 = [i for i inrange(10) if i % 2 == 0] print(list1) # 多個for循環(huán)實(shí)現(xiàn)列表推導(dǎo)式 list1 = [(i, j)fori in range(1, 3)for j in range(3)] print(list1)dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

字典推導(dǎo)式作用: 快速合并列表為字典或提取字典中的目標(biāo)數(shù)據(jù)例如:# 1. 創(chuàng)建一個字典:字典的key是1-5數(shù)字, value是這個數(shù)字的平方 dict1 = {i:i**2 for i in range(1, 5)} print(dict1) # {1: 1, 2: 4, 3: 9, 4: 16} # 2. 將兩個列表合并為一個字典 list1 = ['name', 'age', 'gender'] list2 = ['Tom', 20, 'man'] dict1 = {list1[i]: list2[i] for i inrange(len(list1))} print(dict1) # 3. 提取字典中的目標(biāo)數(shù)據(jù) counts = {'MBP': 268, 'HP': 125, 'DELL': 201, 'Lenovo': 199, 'acer': 99} # 需求: 提取上述電腦數(shù)量大于等于200的字典數(shù)據(jù) count1 = {key: value for key, value in counts.items() if value >= 200} print(count1) # {'MBP':268, 'DELL': 201}dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

集合推導(dǎo)式作用: 快速生成集合,集合有數(shù)據(jù)去重功能例如:# 創(chuàng)建一個集合, 數(shù)據(jù)為下方列表的2次方 list1 = [1, 1, 2] list1 = [1, 1, 2] set1 = {i ** 2 for i in list1} print(set1) # {1, 4}dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

Python中的小整數(shù)對象池和大整數(shù)對象池,以及“is” 和“==”的區(qū)別dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

Python是一門弱變量類型語言,變量使用之前無需聲明變量的類型。對于一個變量有三個屬性:id(內(nèi)存地址),type(變量類型),value(值)dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

對于=, == , is的區(qū)別:= 賦值操作,傳遞的是id, type, value== 判斷的是value是否相等is 判斷id是否相等dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

小整數(shù)對象池對于小整數(shù)對象使用了對象池技術(shù)。設(shè)置小整數(shù)的范圍為[-5,256]。在這個范圍內(nèi)的小整數(shù),任意相同的整數(shù)都是同一個對象,同理,單個字母也是這樣的小整數(shù)的緩沖池是在Python初始化的時候被創(chuàng)建的intern機(jī)制處理空格一個單詞的復(fù)用機(jī)會大,所以創(chuàng)建一次,有空格創(chuàng)建多次,但是字符串長度大于20,就不是創(chuàng)建一次了a = -5 b = -5 a is b # True a = 256 b = 256 a is b # True a = 1000 b = 1000 a is b # True a = 'abc' b = 'abc' a is b # True a = 'helloworld' b = 'helloworld' a is b # True a = 'hello world' b = 'hello world' a is b # FalsedIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

大整數(shù)對象池超出小整數(shù)的范圍即為大整數(shù),每次都會創(chuàng)建一個新的對象。但是處于一個代碼塊的大整數(shù)是同一個對象。終端是每次執(zhí)行一次,所以每次的大整數(shù)都重新創(chuàng)建,而在pycharm中,每次運(yùn)行是所有代碼都加載都內(nèi)存中,屬于一個整體,所以這個時候會有一個大整數(shù)對象池,即處于一個代碼塊的大整數(shù)是同一個對象在pycharm中,每次運(yùn)行是所有代碼都加載都內(nèi)存中,屬于一個整體,所以這個時候會有一個大整數(shù)對象池,即處于一個代碼塊的大整數(shù)是同一個對象a = 1000 b = 1000 a is b # False a = -1888 b = -1888 a is b # False class C1(object): a = 100 b = 100 c = 1000 d = 1000 class C2(objcet): a = 100 b = 1000 print(C1.a is C1.b) # True print(C1.a is C1.a) # True print(C1.c is C1.d) # True print(C1.d is C1.b) # FalsecdIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

生成器dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

生成器的作用根據(jù)程序設(shè)計(jì)者制定的規(guī)則循環(huán)生成數(shù)據(jù),當(dāng)條件不成立時生成數(shù)據(jù)結(jié)束數(shù)據(jù)不是一次性全部生成出來,而是使用一個,再生成一個,可以節(jié)約大量的內(nèi)存dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

生成器的創(chuàng)建生成器推導(dǎo)式生成器推導(dǎo)式與列表推導(dǎo)式相似,不過列表推導(dǎo)式使用小括號# 創(chuàng)建生成器 my_generator = (i * 2 for i in range(5)) print(my_generator)dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

yield 關(guān)鍵字:yield關(guān)鍵字生成器的特性:在def函數(shù)中具有yield關(guān)鍵字defmygenerator(n): for i in range(n): print('開始生成...') yield i print('完成一次...') 注意點(diǎn):代碼執(zhí)行到y(tǒng)ield會暫停,然后把結(jié)果返回出去,下次啟動生成器會在暫停的位置繼續(xù)往下執(zhí)行生成器如果把數(shù)據(jù)生成完成,再次獲取生成器中的下一次數(shù)據(jù)會拋出一個StopIteration異常,表示停止迭代異常while循環(huán)內(nèi)部沒有處理異常操作,需要手動添加處理異常操作for循環(huán)內(nèi)部自動處理了停止迭代異常,使用起來更加方便,dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

生成器相關(guān)函數(shù)next函數(shù)獲取生成器中的下一個值for循環(huán)遍歷生成器中的每一個值dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

相關(guān)免費(fèi)學(xué)習(xí)推薦:編程視頻dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

以上就是Python**詳細(xì)之?dāng)?shù)據(jù)類型講解的詳細(xì)內(nèi)容,更多請關(guān)注少兒編程網(wǎng)其它相關(guān)文章!dIu少兒編程網(wǎng)-Scratch_Python_教程_免費(fèi)兒童編程學(xué)習(xí)平臺

預(yù)約試聽課

已有385人預(yù)約都是免費(fèi)的,你也試試吧...

主站蜘蛛池模板: 寿阳县| 湘西| 襄垣县| 西盟| 包头市| 灵山县| 马公市| 德惠市| 福清市| 佛学| 昌平区| 开江县| 通海县| 盐城市| 七台河市| 湟中县| 侯马市| 本溪| 平乐县| 大荔县| 长春市| 合肥市| 贡觉县| 铁岭市| 岳阳县| 义马市| 锦州市| 托里县| 墨玉县| 南宫市| 东城区| 榆中县| 龙州县| 武城县| 湟源县| 吉林市| 偃师市| 绥宁县| 增城市| 临江市| 广灵县|