搜尋
首頁後端開發Python教學Python最詳細之資料型別講解

Python教學專欄介紹相關資料類型

Python最詳細之資料型別講解

#相關免費學習推薦:python教學(影片)

字串

字串的坑:

三引號的字串如果中間沒有雙引號的字串,會在解釋器中輸出為雙引號

三引號的字串如果中間有雙引號的字串,會在解釋器中輸出為單引號

字串是儲存在記憶體中

字符字串的運算:

輸入:input()

輸出:print()

#索引:str[index]

取位址: id(str )

切片:str[start: end: step] (負數步長-- 倒序選取; 選取方向需一致,否則無法選取資料)

找出:find():語法: str.find(substr, start, end)注意:傳回值是第一次出現的索引值;如果子字串不存在回傳-1,則不會報錯.

例如: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():

語法: str.index(substr, start, end)回傳值是第一次出現的索引值;如果子字串不存在,會錯誤例如: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查找子字串不存在,報錯 count() : 統計子字串出現的次數語法: str.index(substr, tart, end) 注意: 回傳值是子字串出現的次數,如果子字串不存在回傳0,不會報錯例如: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()功能相同,但找出方向為右側開始rindex(): 和index()功能相同,但找出存取為右側開始

Python最詳細之資料型別講解

修改:replace() : 取代語法: str.replace(舊子字串,新子字串, 替換次數)注意:如果不寫替換次數,預設會將舊子字串全部替換替換次數如果超出子字串出現次數,則替換次數為該子字串出現的次數。呼叫replace函數後,發現原有字串的資料並沒有做到修改,修改後的資料是replace函數的回傳值說明: 字串是不可變資料型別例如: 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 表示的是分隔字元出現的次數,即將來臨資料個數為num 1個

#例如mystr = 'hello world and itcast and itheima and Python' list1 = mystr.split('and') list1 = mystr.split('and',2) join():#​​

合併語法:字元或子字串. join(多字元組成的序列) 注意:用一個子字串或子字串合併字串,即是將多個字串合併為一個新的字串例如:mylist = ['aa','bb' ,'cc'] new_str = '...'.join(mylist) print(new_str)

大小寫轉換capitalize() : 將字串第一個字元轉換成大寫語法:str.capitalize( )注意:capitalize()函數轉換後,隻字串第一個字元大寫,其他的字元全都小寫

例如:mystr = 'hello world and itcast and itheima and Python' new_str = mystr.capitalize( ) print(new_str) title(): 將字串每個單字首字母轉換成大寫語法: str.title()

例如:mystr = 'hello world and itcast and itheima and Python' new_str = mystr .title() print(new_str) lower(): 將字串中大寫轉小寫語法: str.lower()注意:全部轉為小寫

例如: mystr = 'hello world and itcast and itheima and Python'new_str = mystr.lower() print(new_str) upper(): 將字串中小寫轉大寫語法: str.upper()注意:全部轉為小寫

#例如:mystr = 'hello world and itcast and itheima and Python' new_str = mystr.upper() print(new_str)

刪除前後空格lstrip(): 刪除字串左側空白字元語法: str.lstrip()例如:mystr = ' hello world and itcast and itheima and Python ' new_str = mystr.lstrip() print(new_str) rstrip(): 刪除字串右側空白字元語法: str.rstrip()###

例如:mystr = ' hello world and itcast and itheima and Python ' new_str = mystr.rstrip() print(new_str) strip(): 刪除字串兩側空白字元語法: str.strip()

例如:mystr = ' hello world and itcast and itheima and Python ' new_str = mystr.strip() print(new_str)

字串對齊ljust(): 傳回一個原始字串左對齊,並使用指定字元(預設空格)填入對應長度的新字串語法: str.ljust(長度,填滿字元)例如:mystr = "hello"print(mystr.ljust(10,'.')) 效果為: ' hello.....' rjust(): 傳回一個原始字串右對齊,並使用指定字元(預設空格)填入對應長度的新字串語法: str.rjust(長度,填滿字元)

例如:mystr = "hello" print(mystr.rjust(10,'.')) 效果為: '.....hello' center():傳回一個原字串居中對齊,並使用指定字元(預設空格)填入對應長度的新字串語法: str.center(長度,填滿字元)例如: mystr = "hello" print(mystr.conter(10,'.')) 效果為: '..hello. ..'

判斷開頭結尾startswith(): 檢查字串是否是以指定子字串開頭,是則傳回True,否則傳回False。如果設定開始和結束位置下標,則在指定範圍內檢查語法: str.startswith(子字串,start,end)

例如: mystr = 'hello world and itcast and itheima and Python' print( mystr.startswith('hello')) print(mystr.startswith('hel')) print(mystr.startswith('hels')) endswith(): 檢查字串是否是以指定子字串結尾,是則傳回True ,否則返回False。如果設定開始和結束位置下標,則在指定範圍內檢查語法:str.endswith(子字串,start,end)

例如:mystr = 'hello world and itcast and itheima and Python' print( mystr.endswith('Python')) print(mystr.endswith('Pythons'))

判斷isalpha(): 如果字串至少有一個字元並且所有字元都是字母則傳回True,否則傳回False語法: str.isalpha()例如: mystr = 'hello world and itcast and itheima and Python' print(mystr.isalpha()) isdigit(): 若字串只包含數字則傳回True,否則回傳Flase.語法: str.isdigit()

例如:mystr1 = '12345' print(mystr1.isdigit()) isalnum(): 如果字串至少有一個字元且所有字元都是字母或數字語法: str.lsalnum ()

例如:mystr2 = 'abc123' print(mystr2.isalnum()) isspace(): 如果字串中只包含空白,則回傳True,否則回傳False.語法: str.isspace()

例如:mystr3 = ' 'print(mystr3.isspace())

清單

清單的坑:

清單是資料結構的一種具體的體現

清單的格式[資料1, 資料2, 資料3]

在工作中,清單盡可能的儲存相同類型的資料

Python最詳細之資料型別講解

清單的操作:

增append(): 清單結尾追加資料語法: list.append(資料)注意:清單追加資料的時候,直接在原清單裡面追加了指定數據,即修改了原始列表,故列表為可變類型資料。如果append()追加的資料是一個序列,則追加整個序列到列表

例如:name_list = ['Tom','Lily','Rost'] name_list.append('xiaoming') print( name_list) # 結果: ['Tom','Lily','Rost','xiaoming'] extend(): 列表結尾追加數據,如果數據是一個序列,則將這個序列的數據逐一新增至清單語法: list.extend(資料)

例如:name_list = ['Tom','Lily','Rost'] name_list.extend('xiaoming') print(name_list) ##結果: ['Tom','Lily','Rost','x','i','a',...] insert(): 指定位置新增資料語法: list.insert(位置下標,數據)例如:name_list = ['Tom','Lily','Rost'] name_list.insert(1, 'xiaoming') print(name_list) ## 結果: ['Tom','xiaoming' ,'Lily','Rost']

判斷是否存在in:語法: str in list

例如:name_list = ['Tom','Lily','Rost' ] print('Lily' inname_list) # 真 print('Lilys' in name_list) # Flase not in:語法: str not in list例如:name_list = ['Tom','Lily','Rost'] print('Lily' not in name_list) # Flase# print('Lilys' not in name_list) # 真

###

刪除:語法: del 目標例如:name_list = ['Tom','Lily','Rost'] del name_list[0] print(name_list) # ['Lily' ,'Rost'] pop(): 刪除指定下標的資料(預設為最後一個),並傳回該資料無論是依照下標或刪除最後一個, pop函數會傳回這個被刪除的資料語法: list .pop()

例如:name_list = ['Tom','Lily','Rost'] del_name = name_list.pop(1) print(del_name) ## 'Lily' print (name_list) # ['Tom','Rost']

remove(): 移除清單中某個資料的第一個符合項目語法: list.remove(資料)

例如: 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) # []

改為修改指定下標資料

例如:name_list = ['Tom','Lily','Rost'] name_list[0] = 'aaa'print(name_list) # ['aaa','Lily','Rost'] reverse() :逆置語法: list.reverse()

例如: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表示排序規則, reverse = True 降序, reverse = False 升序(預設)

#例如:num_list = [1,5,2,3,6,8] num_list.sort() print(num_list ) # [1,2,3,5,6,8]

查下標:index(): 傳回指定資料所在位置的下標-- 如果尋找的資料不存在則錯誤語法: list.index(data,start,end)例如:name_list = ['Tom','Lily','Rose'] print(name_list.index('Lily',0,2))  # 1 count(): 統計指定資料在目前清單中持續的次數語法: list.count()

例如:name_list = ['Tom','Lily','Rose'] print (name_list.count('Lily')) len(): 存取清單長度,即清單中資料的個數語法: len(list)

例如:name_list = ['Tom','Lily', 'Rose'] print(len(name_list)) # 3

複製copy(): 開啟新空間儲存資料語法: list.copy()例如:name_list = ['Tom' ,'Lily','Rose'] name_li2 = name_list.copy() print(name_li2) # ['Tom','Lily','Rose']

# 依次列印清單中的各個資料例如:name_list = ['Tom','Lily','Rose'] i = 0 while i # 結果:Tom Lily Rose for: 依序列印清單中的各個資料例如:name_list = ['Tom','Lily','Rose'] for i # in name_list: print(i) # 結果:Tom Lily Rose

#清單巢狀概念: 清單嵌套指的就是一個清單內包含了其他的子清單

例如:name_list = [['小明','小紅','小綠'],['Tom','Lily','Rose'],['張三','李四', '王二']] print(name_list[2]) # ['張三','李四','王二'] print(name_list[2][1]) #李四

元組

元組的坑:

一個元組可以儲存多個資料, 元組內的資料是不能修改的

元組的格式: (資料1, 資料2, 資料3)

如果定義的元組只有一個資料,那個這個資料後面也最好加逗號, 否則資料型別為唯一的這個資料的資料類型

在工作中,元組盡可能的儲存相同類型的資料

元群組的操作:

#找出按下標查找資料:例如:tuple1 = ('aa','bb','cc','bb') print(tuple1[0]) # aaindex(): 找出某個資料,如果資料存在回傳對應的下標,否則報錯語法: 語法和列表、字串的index方法相同-- tuple.index(子字串)例如:tuple1 = ('aa','bb','cc','bb') print(tuple1 .index('aa')) # 0 count(): 統計某個資料在目前元組出現的次數語法: tuple.count(資料)例如:tuple1 = ('aa','bb ','cc','bb') print(tuple1.count('bb')) # 1len() : 統計元組中資料的個數語法: len(tuple)例如:tuple1 = ('aa','bb','cc','bb') print(len(tuple1)) # 4

#

修改元組內的直接資料若修改則立即報錯例如:tuple1 = ('aa','bb','cc','bb') tuple1[0] = 'aaa'  如果元組裡面有清單,修改清單裡面的資料例如:tuple2 = (10, 20, ['aa', 'bb', 'cc'], 50, 30) print(tuple2[2]) # 訪問到清單 tuple2[2][0] = 'aaaaa' print(tuple2) # (10, 20, ['aaaaa', 'bb', 'cc'], 50, 30)

字典

字典的坑:

字典裡面的資料是以鍵值對形式出現,字典資料和資料順序沒有關係,即字典不支持下標,後期無論資料如何變化,只需要按照對應的鍵的名字查找資料即可

字典的格式: {'key1':value1, 'key2':value2}

字典的操作:

增加dict[key] = value:語法: 字典系列[key] = 值注意: 如果key存在則修改這個key對應的值,如果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))注意:兩個列表的資料個數需要一樣例如: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}

刪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) # {}

#改為修改:語法: 字典序列[key] = 值注意: 如果key存在則修改這個key對應的值,如果key不存在就新增此鍵值對例如:dict1 = {'name':'Tom', 'age':20; 'gender':'男'} dict1['name'] = 'Rose' print (dict1) # {'name':'Rose', 'age':20; 'gender':'男'}

查key查找語法: dict[key]注意:若目前尋找的key存在,則回傳對應的值;否則報錯例如:dict1 = {'name':'Tom', 'age':20; 'gender':'男'} print(dict1['name'] ) # Tomprint(dict1['id']) # 封包錯誤 get()語法: 字典序列.get(key, 預設值) 注意: 如果目前尋找的key不存在則回傳第二個參數(預設值),若省略第二個參數,則回傳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','男')])

####

遍歷字典的key例如dict1 = {'name':'Tom', 'age':20; 'gender':'男'} for key indict1 .keys(): print(key) # 結果: name age gender 遍歷字典的values例如:dict1 = {'name':'Tom', 'age':20; 'gender':'男'} for key in dict1.values(): print(key) # 結果: Tom 20 男 遍歷字典的元素例如:dict1 = {'name ':'Tom', 'age':20; 'gender':'男'} for key in dict1.items(): print(key) # 結果: ('name','Tom') ('age',20) ('gender','男') 遍歷字典的鍵值對(拆包)例如:dict1 = {'name':'Tom ', 'age':20; 'gender':'男'} for key,values in dict1.items(): print(f'{key} = {values} ') # 結果: name = Tom age = 20 gender = 男

#集合

集合的坑:

建立集合使用{}或set (),但是如果要建立空集合只能使用set(),因為使用{}建立的是字典

集合不支援下標運算,因為集合沒有順序

集合資料有去重功能

Python最詳細之資料型別講解

集合的操作:

增加資料add()語法: set.add()注意:因為集合有去重函數,所以,當向集合內追加資料是目前集合已有資料的話, 則不進行任何運算例如:s1 = {10,20} s1.add(100) s1.add(10) print(s1) ## { 100, 10, 20} update():語法: set.update(list)注意: 追加的資料是序列例如:s1 = {10, 20} # s1.update(100) # 報錯 s1.update([100, 200]) s1.update('abc') print(s1) # {'c', 100, 'a', 200, 10, 20, 'b'}

刪除資料remove()語法: set.remove()注意: 刪除集合中的指定數據,如果資料不存在則報錯例如:s1 = {10, 20} s1.remove(10) print(s1) # {20} s1.remove(10) print(s1) # 錯誤# discard()語法: set.discard()注意: 刪除集合中的指定資料, 如果資料不存在也不會報錯例如:s1 = {10, 20} s1.discard(10) print(s1) s1.discard(10) print(s1) pop()語法: set.pop()注意:隨機刪除集合中的某個數據,並傳回這個數據例如:s1 = {10, 20, 30, 40 ,50} del_num = s1.pop() print(del_num) print(s1)

找出資料in : 判斷資料在集合序列not in :判斷資料不在集合序列例如:s1 = {10, 20, 30, 40, 50} print(10 in s1) print(10 not  in s1)

公有運算

運算子

運算子描述支援的容器類型合併字串、清單、元群組*複製字串、列表、元組in元素是否存在字串、列表、元組、字典、集合not in元素是否不存在字串、列表、元組、字典、集合

例如:

# # 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)# True

公用方法

函數描述len()計算容器中元素個數del 或del()刪除max()傳回容器中元素最⼤值min()返回容器中元素最⼩值range(start, end, step)⽣成從start到end的數字,步⻓為step,供for循環使⽤enumerate()函數⽤於將⼀個可遍歷的資料物件(如列表、元組或字串)組合為⼀個索引序列,同時列出資料和資料下標,⼀般⽤在for 迴圈當中

例如:

# 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數字# 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'下標是{index}, 對應的字元是{char}')

容器類型的轉換

tuple()作用: 將某個序列轉換成元組例如list1 = [10, 20, 30, 40, 50, 20] s1 = {100, 200, 300, 400, 500} print(tuple(list1)) print(tuple(s1))

list()作用: 將某個序列轉換成列表例如:t1 = ('a', 'b', 'c', 'd', 'e') s1 = {100, 200, 300, 400, 500} print(list(t1)) print(list(s1))

set()作用: 將某個序列轉換成集合例如:list1 = [10, 20, 30, 40 , 50, 20] t1 = ('a', 'b', 'c', 'd', 'e') print(set(list1)) print(set(t1)) ## 1. 集合可以快速的完成列表去重 # 2. 集合不支援下標

#推導式-- 生成式

列表推導式作用:用一個表達式建立一個規律的清單或控制一個規律清單例如:# while 迴圈實作 # 1. 準備空清單 list1 = [] ## 書寫循環,依序追加數字到空白列表list1中 i = 0 while i # for 迴圈實作 list1 = [ ] for i in range(10): list1.append(i) print(list1) # 清單推導式實作 list1 = [i forfor  i in range(10)] print(list1) # 帶if的列表推導式 # 方法一: range()步長實作 list1 = [i for i in range(0,10,2)] print(list1) # 方法二: 帶if的列表推導式 list1 = [ i for i inrange(10) if i % 2 == 0] print(list1) # 多個for循環實作清單推導式list1 = [(i, j)forin range(1, 3)for j in range(3 )] print(list1)

字典推導式作用: 快速合併清單為字典或提取字典中的目標資料例如:# 1. 建立一個字典:字典的key是1-5數字, value是這個數字的平方 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 in#range(len(list1))} print( dict1) # 3. 提取字典中的目標資料 counts = {'MBP': 268, 'HP': 125, 'DELL': 201, 'Lenovo': 199, 'acer': 99} # 需求: 擷取上述電腦數量大於等於200的字典資料 count1 = {key: value for key, value in counts.items() # if value >= 200} print(count1) # {'MBP':268, 'DELL': 201}

#集合推導式:快速產生集合,集合作用有資料去重功能例如:# 建立一個集合, 資料為下方列表的2次方list1 = [1, 1, 2] list1 = [1, 1, 2] set1 = {i ** 2 for i in list1} print(set1) # {1, 4}

Python中的小整數物件池與大整數物件池,以及「is」 和「==」的區別

Python是一門弱變數類型語言,變數使用之前無需宣告變數的類型。對於一個變數有三個屬性:id(記憶體位址),type(變數型),value(值)

對於=, == , is的區別:= 賦值運算,傳遞的是id, type, value== 判斷的是value是否相等is 判斷id是否相等

小整數物件池對於小整數物件使用了物件池技術。設定小整數的範圍為[-5,256]。在這個範圍內的小整數,任意相同的整數都是同一個對象,同理,單個字母也是這樣的小整數的緩衝池是在Python初始化的時候被創建的intern機制處理空格一個單字的複用機會大,所以創建一次,有空格創建多次,但是字符串長度大於20,就不是創建一次了a = -5 b = -5 a is b # True a = 256 b = 256 a is b # 真 a = 1000 b = 1000 a is b # 真 a = 'abc#is b # True a = 'abc' b = 'abc' a is b # 真# a = 'helloworld' b = 'helloworld' a is b # a = 'hello world' b = 'hello world' a is

 b 

# False大整數物件池超出小整數的範圍即為大整數,每次都會建立一個新的物件。但是處於一個程式碼區塊的大整數是同一個物件。終端是每次執行一次,所以每次的大整數都重新創建,而在pycharm中,每次運行是所有程式碼都載入都記憶體中,屬於一個整體,所以這個時候會有一個大整數物件池,即處於一個程式碼區塊的大整數是同一個物件在pycharm中,每次運行是所有程式碼都載入都記憶體中,屬於一個整體,所以這個時候會有一個大整數物件池,即處於一個程式碼區塊的大整數是同一個物件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) 

## Falsec

生成器產生器的作用根據程式設計者制定的規則循環產生資料,當條件不成立時

產生資料結束

資料不是一次全部生成出來,而是使用一個,再生成一個,可以節約大量的內存生成器的創建生成器推導式生成器推導式與列表推導式相似,但清單推導式使用小括號# 建立產生器 my_generator = (i * 2 

for

 i in range(5)) print(my_generator)yield 關鍵字:yield關鍵字產生器的特性:在def函數中具有yield關鍵字defmygenerator(n): forin range(n): print('開始產生...') 

yield

 i print('完成一次...') 注意點:程式碼執行到yield會暫停,然後把結果回出去,下次啟動生成器會在暫停的位置繼續往下執行生成器如果把數據生成完成,再次獲取生成器中的下一次數據會拋出一個StopIteration異常,表示停止迭代異常while循環內部沒有處理異常操作,需要手動添加處理異常操作for循環內部自動處理了停止迭代異常,使用起來更加方便,

生成器相關函數next函數
取得生成器中的下一個值

for循環遍歷產生器中的每一個值

相關免費學習推薦:###程式設計影片##### ####

以上是Python最詳細之資料型別講解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:简书。如有侵權,請聯絡admin@php.cn刪除
學習Python:2小時的每日學習是否足夠?學習Python:2小時的每日學習是否足夠?Apr 18, 2025 am 12:22 AM

每天學習Python兩個小時是否足夠?這取決於你的目標和學習方法。 1)制定清晰的學習計劃,2)選擇合適的學習資源和方法,3)動手實踐和復習鞏固,可以在這段時間內逐步掌握Python的基本知識和高級功能。

Web開發的Python:關鍵應用程序Web開發的Python:關鍵應用程序Apr 18, 2025 am 12:20 AM

Python在Web開發中的關鍵應用包括使用Django和Flask框架、API開發、數據分析與可視化、機器學習與AI、以及性能優化。 1.Django和Flask框架:Django適合快速開發複雜應用,Flask適用於小型或高度自定義項目。 2.API開發:使用Flask或DjangoRESTFramework構建RESTfulAPI。 3.數據分析與可視化:利用Python處理數據並通過Web界面展示。 4.機器學習與AI:Python用於構建智能Web應用。 5.性能優化:通過異步編程、緩存和代碼優

Python vs.C:探索性能和效率Python vs.C:探索性能和效率Apr 18, 2025 am 12:20 AM

Python在開發效率上優於C ,但C 在執行性能上更高。 1.Python的簡潔語法和豐富庫提高開發效率。 2.C 的編譯型特性和硬件控制提升執行性能。選擇時需根據項目需求權衡開發速度與執行效率。

python在行動中:現實世界中的例子python在行動中:現實世界中的例子Apr 18, 2025 am 12:18 AM

Python在現實世界中的應用包括數據分析、Web開發、人工智能和自動化。 1)在數據分析中,Python使用Pandas和Matplotlib處理和可視化數據。 2)Web開發中,Django和Flask框架簡化了Web應用的創建。 3)人工智能領域,TensorFlow和PyTorch用於構建和訓練模型。 4)自動化方面,Python腳本可用於復製文件等任務。

Python的主要用途:綜合概述Python的主要用途:綜合概述Apr 18, 2025 am 12:18 AM

Python在數據科學、Web開發和自動化腳本領域廣泛應用。 1)在數據科學中,Python通過NumPy、Pandas等庫簡化數據處理和分析。 2)在Web開發中,Django和Flask框架使開發者能快速構建應用。 3)在自動化腳本中,Python的簡潔性和標準庫使其成為理想選擇。

Python的主要目的:靈活性和易用性Python的主要目的:靈活性和易用性Apr 17, 2025 am 12:14 AM

Python的靈活性體現在多範式支持和動態類型系統,易用性則源於語法簡潔和豐富的標準庫。 1.靈活性:支持面向對象、函數式和過程式編程,動態類型系統提高開發效率。 2.易用性:語法接近自然語言,標準庫涵蓋廣泛功能,簡化開發過程。

Python:多功能編程的力量Python:多功能編程的力量Apr 17, 2025 am 12:09 AM

Python因其簡潔與強大而備受青睞,適用於從初學者到高級開發者的各種需求。其多功能性體現在:1)易學易用,語法簡單;2)豐富的庫和框架,如NumPy、Pandas等;3)跨平台支持,可在多種操作系統上運行;4)適合腳本和自動化任務,提升工作效率。

每天2小時學習Python:實用指南每天2小時學習Python:實用指南Apr 17, 2025 am 12:05 AM

可以,在每天花費兩個小時的時間內學會Python。 1.制定合理的學習計劃,2.選擇合適的學習資源,3.通過實踐鞏固所學知識,這些步驟能幫助你在短時間內掌握Python。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 個月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
1 個月前By尊渡假赌尊渡假赌尊渡假赌
威爾R.E.P.O.有交叉遊戲嗎?
1 個月前By尊渡假赌尊渡假赌尊渡假赌

熱工具

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器