Python程式設計入門學習筆記(三)
阿新 • • 發佈:2018-12-25
### 切片 ```python line = 'Welcome to Beijing,welcome to China!' #取字串的前10個字元,line[0:10],預設是0 line[:10] ``` 'Welcome to' ```python #取第一個字元 line[1] ``` 'e' ```python #從0開始到20個字元,每2個取一個字元 line[0:20:2] ``` 'Wloet ejn,' ### 取後10個字元 ```python line[-10:] ``` ' to China!' ### 反轉字元 ```python line[::-1] ``` '!anihC ot emoclew,gnijieB ot emocleW' ### 單字元 ### 單字元是不可以進行賦值的 ```python line[-1] = 'E' ``` --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-38-ea6925c27361> in <module>() ----> 1 line[-1] = 'E' TypeError: 'str' object does not support item assignment ```python #第一個字元大寫,其他的字元小寫 line.capitalize() ``` 'Welcome to beijing,welcome to china!' ```python #居中處理,空白字元以‘#’代替 line.center(40,'#') ``` '##Welcome to Beijing,welcome to China!##' ```python #計數函式 line.count('e') ``` 5 ### 字串首尾判斷 ```python #是否以給定的字串結尾 line.endswith('na!') ``` True ```python #是否以給定的字串開頭 line.startswith('we') ``` False ```python #返回給定字元的第一個位置下標,如果字元不存在,返回‘-1’ line.find('l') ``` 2 ```python #從指定的位置開始,返回給定字元的位置下標,如果字元不存在,返回‘-1’ line.find('l',3) ``` 21 ```python #返回給定字元的第一個位置下標,如果字元不存在,報錯 line.index('l') ``` 2 ```python #將字串所有字元大寫 line.upper() ``` 'WELCOME TO BEIJING,WELCOME TO CHINA!' ```python #將字串所有字元小寫 line.lower() ``` 'welcome to beijing,welcome to china!' ```python #判斷是否是首字母大寫,其他小寫的格式 line.istitle() ``` False ```python #判斷是否全部是大寫 line.isupper() ``` False ```python #判斷是否全是小寫 line.islower() ``` False ```python line = ' Ls \n Kdk \n\t ' ``` ```python #去掉字串兩端的空格,製表符,中間的是無法去掉的 line.strip() ``` 'Ls \n Kdk' ```python #去掉字串右邊的空格,製表符, line.rstrip() ``` ' Ls \n Kdk' ```python #去掉字串左邊的空格,製表符, line.lstrip() ``` 'Ls \n Kdk \n\t ' ```python #大小寫相互轉換,即大寫轉小寫,小寫轉大寫 line.swapcase() ``` ' lS \n kDK \n\t ' ### 【重點】上面用到的所有字串函式,都是重新生成了一個新的字串,原有的字串是不變的 ```python line = 'ni hao' id(line) ``` 2287933149848 ```python new_line = line.upper() id(line),id(new_line) ``` (2287933149848, 2287933519216) ### 列表 ```python # 空列表 varibals = [] varibals = list[] ``` #### 可以容納任意型別的物件,任意數量的物件【重點】列表是可變型別的 ```python varibals = [1, 2, 3, 'ni hao', 'hello,python', [], [100,100]] ``` ```python varibals = [] varibals.append(1) varibals.append(2) varibals.append('ni hao') varibals ``` [1, 2, 'ni hao'] ```python varibals[0] = 10 varibals ``` [10, 2, 'ni hao'] #### python是一種動態型別的語言,一個變數是什麼型別,要看程式在執行過程中變數所代表的值是什麼 ```python var = 10 type(var) var = 'str' type(var) ``` str ### 切片 ```python varibals[-2:] ``` [2, 'ni hao'] ```python varibals + [1,23] ``` [10, 2, 'ni hao', 1, 23] ```python varibals * 4 ``` [10, 2, 'ni hao', 10, 2, 'ni hao', 10, 2, 'ni hao', 10, 2, 'ni hao'] ### 序列 列表是一種容器型的序列;字串是一種扁平型的序列 ```python len(varibals) ``` 3 ```python #沒有返回值,而是修改了列表物件本身 varibals.append(1) varibals ``` [10, 2, 'ni hao', 1] ```python # 清空 varibals.clear() ``` ```python varibals = [1,12,23,4234,[1, 2]] ``` ```python #copy的作用是不改變原來變數的值 new_varibals = varibals.copy() ``` ```python #淺複製 new_varibals[0] = 9999 ``` ```python new_varibals ``` [9999, 12, 23, 4234] ```python varibals ``` [1, 12, 23, 4234] ```python #深複製 new_varibals[-1][0] = 9999 new_varibals ``` [1, 12, 23, 4234, [9999, 2]] ```python varibals ``` [1, 12, 23, 4234, [9999, 2]] ```python id(new_varibals[-1]),id(varibals[-1]) ``` (2287932939720, 2287932939720) ```python a = [1, 2] b = [3 ,4] a + b ``` [1, 2, 3, 4] ```python a.extend(b) a ``` [1, 2, 3, 4, 3, 4] ```python #在列表指定位置插入一個值 a.insert(0,100) a ``` [100, 1, 2, 3, 4, 3, 4] ```python # 把列表指定位置的元素彈出,不指定引數則彈出最後一個值 a.pop(0) ``` 100 ```python a ``` [1, 2, 3, 4, 3, 4] ```python #移除一個指定元素,如果元素不存在則報錯 a.remove(3) a ``` [1, 4, 3, 4] ```python #排序函式,reverse排序反轉 a.sort(reverse=True) a ``` [4, 4, 3, 1] ```python #in的用法 4 in a ``` True ### tuple 不可變列表 ```python var = tuple() var = () type(var) ``` tuple ```python # 只有count,index兩個函式 var = (1,2,1,3,4,5,[23,34,43]) ``` ```python var.count(1) ``` 2 ```python var.index(5) ``` 5 ```python a = 10, 20 a ``` (10, 20) ### 字典型別 | 元祖變數 | 字串變數 | 列表變數 | | --- | --- | --- | | t_1 = [1,2,3,4,5] | s_1 = 'ni hao' | l_1 = [1,2,3,4,5] | | t_2 = [5,6,7,8,9] | s_2 = 'how are you' | l_2 = [6,7,8,9,10] | | 函式 | 元祖 | 例項 | 字串 | 例項 | 列表 | 例項 | | :--- | :---: | :--- | :---: | :--- | :---: | :--- | :--- | | + | √ | t_1 + t_2 | √ | s_1 + s_2 | √ | l_1 + l_2 | | * | √ | t_1 * 2 | √ | s_1 * 2 | √ | l_1 * 2 | | > < | √ | t_1 > t_2 | √ | s_1 > s_2 | √ | l_1 > l_2 | | [index] | √ | t_1[0] | √ | s_1[0] | √ | l_1[0] | 列表可以索引賦值,字串、元祖不可以 | | [::] | √ | t_1[::] | √ | s_1[0:10:1] | √ | l_1[0:10:2] |列表可以切片賦值,字串、元祖不可以| | len | √ | len(t_1) | √ | len(s_1) | √ | len(l_1) | |bool|√|bool(t_1)|√|bool(s_1)|√|bool(l_1)|空字串、空列表、空元祖轉換為布林型為False| | count | √ | t_1.count(1) | √ | s_1.count('n') | √ | l_1.count(1) | | index | √ | t_1.index(3) | √ | s_1.index('n') | √ | l_1.index(1) | | replace | | | √ | s_1.replace('n','N') | | | 字串replace函式返回一個新字串,原來的變數不變 | | sort | | | | | √ | l_1.sort(100) | | reverse | | | | | √ | l_1.reverse() | 字串不可改變,只能通過生成一個新的字串來翻轉 | | append | | | | | √ | l_1.append(100) | | extend | | | | | √ | l_1.extend(l_2) | | remove | | | | | √ | l_1.remove(1) | | pop | | | | | √ | l_1.pop() | ### 字典型別 ```python var = {} var = dict() type(var) ``` dict ```python var = { '中':100, '左':200 } ``` ```python var['中'] ``` 100 ```python words = ['中','左'] location = [100,200] location[words.index('中')] ``` 100 #### 拉鎖函式 ```python new_var = list(zip(words,location)) ``` ```python dict(new_var) ``` {'中': 100, '左': 200} ```python list(zip([1,2],[3,4],[4,5,6])) ``` [(1, 3, 4), (2, 4, 5)] ```python students = ['wang','li','sun','zhao','qian'] ``` ```python #fromkeys為list元素賦初始值 money = dict.fromkeys(students,1000) ``` #### 訪問字典中的值 ```python money['wang'] ``` 1000 ```python #用get找名為ww的元素,如果找不到,返回none,如果需要初始值,可以通過引數填加,如100。 a = money.get('ww','100') print(a) ``` 100 ```python money.keys() ``` dict_keys(['wang', 'li', 'sun', 'zhao', 'qian']) ```python money.values() ``` dict_values([1000, 1000, 1000, 1000, 1000]) ```python money.items() ``` dict_items([('wang', 1000), ('li', 1000), ('sun', 1000), ('zhao', 1000), ('qian', 1000)]) ```python #刪除操作 money.pop('wang') ``` 1000 ```python money ``` {'li': 1000, 'sun': 1000, 'zhao': 1000, 'qian': 1000} ```python money['wang'] = 100 ``` ```python money ``` {'li': 1000, 'sun': 1000, 'zhao': 1000, 'qian': 1000, 'wang': 100} ```python #setdefault如果原來存在值,則返回原來的值,如果不存在則返回引數的值 money.setdefault('li',5000) ``` 1000 ```python money.setdefault('gao',5000) ``` 5000 ```python money ``` {'li': 1000, 'sun': 1000, 'zhao': 1000, 'qian': 1000, 'wang': 100, 'gao': 5000}