for 循環提供了一種手段,能夠不依靠索引取值。git
語法:api
''' for 變量名(會拿到容器類元素的每個值,沒有了就結束循環) in 容器類元素: print(變量名) '''
例如:app
打印一個列表內的全部元素:函數
lt = [1, 2, 3, 4] for i in lt: print(i)
運行結果:code
1 2 3 4 Process finished with exit code 0
打印字典內的元素:對象
dic = {'a': 1, 'b': 2, 'c': 3} count = 0 for i in dic: # 對於字典,for循環只能拿到key print(i, dic[i]) count += 1
運行結果:排序
a 1 b 2 c 3 Process finished with exit code 0
range()索引
# for i in range(1,10,2): # range(起始,結束,步長),顧頭不顧尾 # print(i)
運行結果:ip
1 3 5 7 9 Process finished with exit code 0
for…break:提早終止循環
只打印1-10的前兩個數:
for i in range(1,11): if i == 3: break # 中斷循環 print(i)
運行結果:
1 2 Process finished with exit code 0
for…continue:跳出本次循環,不執行下面的代碼
打印1-5,可是不打印3:
for i in range(1,6): if i == 3: continue # 跳出本次循環,不執行下面的代碼 print(i)
運行結果:
1 2 4 5 Process finished with exit code 0
for…else(瞭解:for循環不被break終止就執行else下的代碼,不然不執行)
例如:
打印1-5:
for i in range(1,6): print(i) else: print('沒有被break,我就能出來。')
運行結果:
1 2 3 4 5 沒有被break,我就能出來。
打印1-5,當打印到2的時候,終止循環:
for i in range(1,6): if i == 3: break print(i) else: print('沒有被break,我就能出來。')
運行結果:
1 2 Process finished with exit code 0
練習
使用代碼是實現 Load......
的動態效果。
實現代碼:
import time print('Loading', end='') for i in range(6): print('.', end='') time.sleep(0.5)
整形:
做用
年齡、ID……
定義方式
a = 1
或 a = int(1)
使用方法
+ - * / % // ** < <= > >= == !=
有序 or 無序(有索引的就有序,無索引的就無序)
整型沒有有序無序的概念。
可變 or 不可變(值變id不變是可變,值變id變是不可變)
整形是不可變類型。
浮點型:
做用
薪資……
定義方式
a = 1.1
或 a = float(1.1)
使用方法
+ - * / % // ** < <= > >= == !=
有序 or 無序(有索引的就有序,無索引的就無序)
浮點型沒有有序無序的概念。
可變 or 不可變(值變id不變是可變,值變id變是不可變)
浮點型是不可變類型。
做用
姓名……
定義方式: str
單引號/雙引號/三單引號/三雙引號:s = '字符串'
使用方法
優先掌握
索引取值:[字符位置]
正序從第一個字符以 0 開始;倒序從最後一個字符開始以 -1 開始
s = '一二三四' print(s[0]) # 取出第一個字符:一
結果:
一 Process finished with exit code 0
切片:[起始位置:結束位置:步長]
s = '一二三四五六七八九十' print(s[0:4]) # 顧頭不顧尾 print(s[0:8:2]) # 步長2,隔一個取一個 print(s[4:0:-1]) # +從左到右,-表示從右到左 print(s[2:]) # 左邊的不寫取到最左邊,右邊的不寫取到最右邊
運行結果:
一二三四 一三五七 五四三二 三四五六七八九十 Process finished with exit code 0
遍歷:for 循環
s = '一二三' for i in s: print(i)
運行結果:
一 二 三 Process finished with exit code 0
成員運算: in
/ not in
(返回一個布爾值 True 或 False )
s='一二三四' print('一' in s) # 判斷:一是否是在字符串s裏面 print('二' not in s) # 判斷:二是否是不在字符串s裏面
運行結果:
True False Process finished with exit code 0
strip()
:默認去除兩端空格,能夠指定去除的字符,能夠指定多個字符同時去掉
s = ' 一二三**' print(s.strip()) print(s.strip(' 一*'))
運行結果:
一二三** 二三 Process finished with exit code 0
切割: split()
按照對象切割字符串,獲得的一個列表
s = '一|二|三|四' print(s.split('|'))
運行結果:
['一', '二', '三', '四'] Process finished with exit code 0
長度: len()
:得到字符串的長度
s = '一二三四' print(len(s))
運行結果:
4 Process finished with exit code 0
須要掌握
lstrip
和 rstrip
s = '**tom**' print(s.lstrip('*')) print(s.rstrip('*'))
運行結果:
tom** **tom Process finished with exit code 0
lower
和 upper
s = 'Tom' print(s.lower()) # 改成全小寫 print(s.upper()) # 改成全大寫
運行結果:
tom TOM Process finished with exit code 0
startswith
和 endswith
s = 'Tom' print(s.startswith('T')) # 以……開始,返回布爾值 print(s.endswith('f')) # 以……結束,返回布爾值
運行結果:
True False Process finished with exit code 0
rsplit
s = '一|二|三|四' print(s.rsplit('|', 2)) # 2表示只切割兩個對象 print(s.split('|', 2))
運行結果:
['一|二', '三', '四'] ['一', '二', '三|四'] Process finished with exit code 0
join
:拼接列表內的字符串
s = '1|2|3|4|5' lt = s.split('|') # 將字符串切割成列表 print('*'.join(lt)) # 將列表內的元素用*鏈接起來
運行結果:
1*2*3*4*5 Process finished with exit code 0
replace
s = '一二三' s = s.replace('一', '五') print(s)
運行結果:
五二三 Process finished with exit code 0
isdigit
和 isalpha
s = '123456' print(s.isdigit()) # 判斷字符串內字符是否都爲數字,返回布爾值 print(s.isalpha()) # 判斷字符串內字符是否都爲字母,返回布爾值
運行結果:
True False Process finished with exit code 0
瞭解
find|rfind|index|rindex|count
s = '一二三四五一一一' print(s.find('二', 1, 5)) # 查找字符串的索引位置(1,5表示從索引1開始查找到索引5) print(s.rfind('二')) # 和 find 的做用徹底同樣 print(s.find('六')) # 當查找不到字符時,返回 -1 print(s.index('二')) # 查找字符的索引位置 print(s.count('一')) # 計算字符的個數 # print(s.index('六')) # 當查找不到字符時,程序會報錯
運行結果:
1 1 -1 1 4 Process finished with exit code 0
center|ljust|rjust|zfill
s = 'title' print(s.center(10, '*')) # 設置輸出長度和填充字符,將內容居中打印 print(s.ljust(10, '*')) # 設置輸出長度和填充字符,將內容居左打印 print(s.rjust(10, '*')) # 設置輸出長度和填充字符,將內容居右打印 print(s.zfill(10)) # 設置輸出長度,在內容前面用0填充
運行結果:
**title*** title***** *****title 00000title Process finished with exit code 0
expandtabs
s = 'a\ta' print(s) print(s.expandtabs(6)) #調整縮進的尺寸
運行結果:
a a a a Process finished with exit code 0
captalize|swapcase|title
s = 'tom bruce' print(s.capitalize()) # 將字符串中的英文改成首字母大寫 print(s.swapcase()) # 將字符串中的英文大寫改成小寫,小寫改成大寫 print(s.title()) # 將字符串中的每一個單詞的首字母大寫
運行結果:
Tom bruce TOM BRUCE Tom Bruce Process finished with exit code 0
is系列…
有序 or 無序(有索引的就有序,無索引的就無序)
有序。
可變 or 不可變(值變id不變是可變,值變id變是不可變)
不可變。
做用
存儲任意類型的多個元素。
定義方式: list
lt = [1, 2, 3, 4]
使用方法
優先掌握
索引取值: [元素位置]
lt = [1, 2, 3, 4] print(lt[1])
運行結果:
2 Process finished with exit code 0
索引修改值: [元素位置] = 新值
lt = [1, 2, 3, 4] lt[1] = 3 print(lt)
運行結果:
[1, 3, 3, 4] Process finished with exit code 0
切片:[起始位置:結束位置:步長]
lt= [1,2,3,4,5,6,7] print(lt[:]) # 不寫默認從開始到結尾 print(lt[2:5]) # 顧頭不顧尾 print(lt[1:6:2]) # 從位置1開始到位置6結束,每隔1個元素取一個
運行結果:
[1, 2, 3, 4, 5, 6, 7] [3, 4, 5] [2, 4, 6] Process finished with exit code 0
遍歷:for 循環
lt = [1, 2, 3, 4] for i in lt: # 將列表內的元素逐一取出打印 print(i)
運行結果:
1 2 3 4 Process finished with exit code 0
成員運算: in
/ not in
(返回一個布爾值 True 或 False )
lt = [1, 2, 3, 4] print(1 in lt) print(5 in lt)
運行結果:
True False Process finished with exit code 0
長度: len()
:得到字符串的長度
lt = [1, 2, 3, 4] print(len(lt))
運行結果:
4 Process finished with exit code 0
添加元素: append(值)
lt = [1, 2, 3, 4] lt.append(5) print(lt)
運行結果:
[1, 2, 3, 4, 5] Process finished with exit code 0
刪除元素: del[位置]
lt = [1, 2, 3, 4] del lt[0] print(lt)
運行結果:
[2, 3, 4] Process finished with exit code 0
須要掌握
插入元素:insert(插入位置,插入元素)
lt = [1, 2, 3, 4] lt.insert(2, 0) # 在位置 2 前插入 0 print(lt)
運行結果:
[1, 2, 0, 3, 4] Process finished with exit code 0
按照索引刪除值: pop(位置)
lt = [1, 2, 3, 4] lt.pop(2) print(lt)
運行結果:
[1, 2, 4] Process finished with exit code 0
按照值刪除值: remove(刪除對象)
lt = [1, 2, 3, 4] lt.remove(2) print(lt)
運行結果:
[1, 3, 4] Process finished with exit code 0
計數: count()
lt = [1, 1, 1, 2] print(lt.count(1))
運行結果:
3 Process finished with exit code 0
尋找值的索引: index()
lt = [1, 2, 3, 4] print(lt.index(1)) # 找到了返回索引值,找不到報錯
運行結果:
0 Process finished with exit code 0
清空列表: clear()
lt = [1, 2, 3, 4] lt.clear() print(lt)
運行結果:
[] Process finished with exit code 0
拷貝列表: copy()
lt = [1, 2, 3, 4] lt1 = lt.copy() print(lt1)
運行結果:
[1, 2, 3, 4] Process finished with exit code 0
擴展列表: extend()
lt1 = [1, 2, 3] lt2 = [4, 5, 6] lt1.extend(lt2) print(lt1)
運行結果:
[1, 2, 3, 4, 5, 6] Process finished with exit code 0
反轉列表: reverse()
lt = [1, 2, 3, 4] lt.reverse() print(lt)
運行結果:
[4, 3, 2, 1] Process finished with exit code 0
元素排序: sort()
lt = [2, 3, 1, 0, 4] lt.sort() # 從小到大排序 # lt.sort(reverse=True) # 從大到小排序 print(lt)
運行結果:
[0, 1, 2, 3, 4] # [4, 3, 2, 1, 0] Process finished with exit code 0
有序 or 無序(有索引的就有序,無索引的就無序)
有序。
可變 or 不可變(值變id不變是可變,值變id變是不可變)
可變。
方法是解釋器內部定義好的函數。
例如咱們能夠本身寫一個排序方法:
class SelfList(list): def self_sort(self): for i in range(len(self)): # [0,1,2,3,4,5] for j in range(len(self)): # [0,1,2,3,4,5] if self[i] < self[j]: self[i], self[j] = self[j], self[i] lt = SelfList([2, 0, 4, 3, 5]) lt.sort() print(lt)
運行結果:
[0, 2, 3, 4, 5] Process finished with exit code 0