-
單引號(hào)和雙引號(hào)嵌套使用可以有效避免轉(zhuǎn)義帶來的麻煩:
"this is 'a' String" 'this is also "a" String' 'this is 'a'nother String' #你能看到 a 的顏色不對(duì) "this is also "a"nother String" #你看,a 的顏色又不對(duì) -
三引號(hào)會(huì)包含介于其中的所有換行,包括三引號(hào)自身帶來的換行:
string = """ ab c """ //string = '\nab\nc\n' Python 的變量名可以用中文。
print()函數(shù)可以同時(shí)接受很多個(gè)不同類型參數(shù),用逗號(hào),分隔。會(huì)一個(gè)一個(gè)的打印,輸出的句子中不同元素間默認(rèn)使用空格分隔。字符串能容納一定長(zhǎng)度的字符。元組和列表可以容納若干個(gè)一切類型的元素。字典接受若干個(gè)鍵值對(duì),鍵的類型相同,值的類型相同。字符串用
''、""、''' '''、""" """包括,元組用()包括,列表用[]包括,字典用{}包括。此外還有一種容器,內(nèi)容不能重復(fù),它的名字叫做集合,也用{}包括。以上容器內(nèi)部的元素之間用逗號(hào)分隔。-
字符串可以插值。我使用插值這個(gè)詞,是因?yàn)檫@個(gè)定義在 Swift 中常見,英文叫 string interpolation,我個(gè)人覺得在中文中“插值”要比“格式化”更易理解。在 C 中經(jīng)常在
scanf()和printf()函數(shù)中使用,在 Python 中也有類似的用法,不過其實(shí)還有更加好用的format()函數(shù):name = 'Houston Duane' age = 20 #插值方法一:使用format()函數(shù) string1 = "My name is {} and I'm {} years old." print(string1.format(name,age)) #插值方法二:使用格式化符號(hào) string2 = "My name is %s and I'm %d years old." print(string2 % (name,age))從上面可以看出,
format()函數(shù)會(huì)在花括號(hào)的位置無腦替換內(nèi)容,格式化符號(hào)則需要小心選取合適的變量類型 (%s代表字符串,%d代表整數(shù))。因此,在通常使用上,format()函數(shù)要更好用一些。但是格式化符號(hào)也有自己的優(yōu)勢(shì),它可以更進(jìn)一步的對(duì)插值內(nèi)容進(jìn)行格式化,例如固定寬度、前面填0、使用八進(jìn)制和十六進(jìn)制、使用科學(xué)技術(shù)法等。 -
字符串、列表和元組的讀取和拼接:
example_str = '我是字符串' example_tuple = ('我','是','元','組') example_list = ['我','是','列','表'] #取容器中的單個(gè)元素 #正序的時(shí)候下標(biāo)從0開始遞增 print(example_str[2]) #字 print(example_tuple[2]) #元 #逆序的時(shí)候下標(biāo)從-1開始遞減 print(example_list[-1]) #表 print(example_str[-2]) #符 #取容器的子集 #print(example_str[a:b:c]) a是開始下標(biāo),b是結(jié)束下標(biāo)的下一位,c是步長(zhǎng)及方向 #c的默認(rèn)值是1 #a的默認(rèn)值看c,如果c為正,則a默認(rèn)為0;如果c為負(fù),則a默認(rèn)為-1 #b的默認(rèn)值和c的符號(hào)相同,絕對(duì)值是len(example_str) #若a,b,c有省略則按照規(guī)則填充默認(rèn)值 #明確首尾 print(example_list[2:4]) #['列', '表'] print(example_tuple[1:3]) #('是', '元') print(example_str[-1:-3]) # $打印空字符,因?yàn)閇-1:-3:1]不合法 print(example_list[-1:0:]) #[] $打印空列表,因?yàn)閇-1:0:1]不合法 print(example_str[-1:-3:-1]) #串符 $倒序的步長(zhǎng)必須顯式指出 print(example_list[-1:0:-1]) #['表', '列', '是'] $雖然看起來不合法,但其實(shí)合法 #首尾有省略 print(example_list[-2:]) #['列', '表'] print(example_str[2:]) #字符串 print(example_list[:2]) #['我', '是'] print(example_str[:-1:]) #我是字符 #特殊步長(zhǎng) print(example_tuple[::-1]) #('組', '元', '是', '我') $逆序輸出 print(example_str[::2]) #我字串 $步長(zhǎng)取2的意思是正序每2位取1位 print(example_list[1:3:2]) #['是'] print(example_str[-1:-5:-3]) #串是 $步長(zhǎng)取-3的意思是逆序每3位取1位 print(example_tuple[-1::-2]) #('組', '是') #元組的拼接 a = (2,3,1) b = (4,2,6) c = a + b print(c) #(2, 3, 1, 4, 2, 6) -
字符串和元組只可以做讀、切片和拼接操作;而列表、集合、字典可以做到讀、切片、擴(kuò)容、修改和刪除。元組、列表可以轉(zhuǎn)化為集合,轉(zhuǎn)化過程中會(huì)去重,得到的集合里面的排列順序不一定與原來容器里面的相同。反過來把集合轉(zhuǎn)為列表或者元組,順序則按照集合里的順序。
#列表的擴(kuò)容 example_list.append(example_tuple) print(example_list) #['我', '是', '列', '表', ('我', '是', '元', '組')] #列表的修改 example_list[1] = '不是' print(example_list) #['我', '不是', '列', '表', ('我', '是', '元', '組')] #列表的刪除 example_list.pop(0) print(example_list) #['不是', '列', '表', ('我', '是', '元', '組')] #將列表轉(zhuǎn)為集合,觀察去重和亂序 example_list.append('不是') print(example_list) #['不是', '列', '表', ('我', '是', '元', '組'), '不是'] example_set = set(example_list) print(example_set) #{('我', '是', '元', '組'), '不是', '表', '列'} #此時(shí)把集合再轉(zhuǎn)為元組的時(shí)候,順序則嚴(yán)格按照原集合里的順序排列 example_tuple = tuple(example_set) print(example_tuple) #(('我', '是', '元', '組'), '不是', '表', '列') #向集合中添加元素,順序依舊會(huì)亂 example_set.add(3.14) print(example_set) #{('我', '是', '元', '組'), '不是', 3.14, '列', '表'} #字典的擴(kuò)容 example_dict = { 'name':'Houston', 'job':'iOS Developer', 'company':'ByteDance Shenzhen' } example_dict['section']='EE' print(example_dict) '''{ 'name': 'Houston', 'job': 'iOS Developer', 'company': 'ByteDance Shenzhen', 'section': 'EE' }''' #字典的修改 example_dict['name']='Houston Duane' print(example_dict) '''{ 'name': 'Houston Duane', 'job': 'iOS Developer', 'company': 'ByteDance Shenzhen', 'section': 'EE' }''' #字典的刪除 example_dict.pop('company') print(example_dict) '''{ 'name': 'Houston Duane', 'job': 'iOS Developer', 'section': 'EE' }''' #字典中后出現(xiàn)的鍵值對(duì)會(huì)替換掉前面相同鍵的值 example_dict = {4:'0100', 3:'0011', 3:'0x3', 2:'0x2'} print(example_dict) #{4: '0100', 3: '0x3', 2: '0x2'} -
if-elif-else 語句:
if 'name' in example_dict and 'company' in example_dict: print('I know %s works for %s.' % (example_dict['name'], example_dict['company'])) elif 'name' in example_dict and \ 'company' not in example_dict and \ 'section' in example_dict: print("I don't know where %s works, but I know %s works in %s." % (example_dict['name'], example_dict['name'], example_dict['section'])) else: print("I don't know nothing.") #I don't know where Houston Duane works, but I know Houston Duane works in EE.if可以搭配 0 個(gè)或多個(gè)elif和 0 個(gè)或 1 個(gè)else來組成條件語句。 -
如果需要很多很多個(gè)
elif的話,其實(shí)可以用字典來簡(jiǎn)化條件:new_iPhone_price = { 'iPhone XR (64GB)': 6_499, 'iPhone XR (128GB)': 6_999, 'iPhone XR (256GB)': 7_899, 'iPhone XS (64GB)': 8_699, 'iPhone XS (256GB)': 10_099, 'iPhone XS (512GB)': 11_899, 'iPhone XS Max (64GB)': 9_599, 'iPhone XS Max (256GB)': 10_999, 'iPhone XS Max (512GB)': 12_799 } print(new_iPhone_price['iPhone XR (128GB)']) #6999 print(new_iPhone_price.get('iPhone XR (64GB)')) #6499 #可以自定義當(dāng)鍵不存在時(shí)返回的默認(rèn)值 print(new_iPhone_price.get('iPhone X (64GB)', 'Not On Sale')) #Not On Sale -
Python 中有
in、not、and、or、range()可以幫助使用條件語句和循環(huán)語句。a in b代表a在b里。not and or分別代表邏輯上的非、與、或。range()則用于表明一個(gè)范圍:#默認(rèn)輸出鍵 for key in new_iPhone_price: print (key) ''' iPhone XR (64GB) iPhone XR (128GB) iPhone XR (256GB) iPhone XS (64GB) iPhone XS (256GB) iPhone XS (512GB) iPhone XS Max (64GB) iPhone XS Max (256GB) iPhone XS Max (512GB) ''' #可以明確輸出鍵還是值 for key in new_iPhone_price.keys(): print (key) ''' iPhone XR (64GB) iPhone XR (128GB) iPhone XR (256GB) iPhone XS (64GB) iPhone XS (256GB) iPhone XS (512GB) iPhone XS Max (64GB) iPhone XS Max (256GB) iPhone XS Max (512GB) ''' for val in new_iPhone_price.values(): print (val) ''' 6499 6999 7899 8699 10099 11899 9599 10999 12799 ''' #當(dāng)然可以輸出鍵值對(duì) for key,val in new_iPhone_price.items(): print ("%s: %d" % (key,val)) ''' iPhone XR (64GB): RMB 6499 iPhone XR (128GB): RMB 6999 iPhone XR (256GB): RMB 7899 iPhone XS (64GB): RMB 8699 iPhone XS (256GB): RMB 10099 iPhone XS (512GB): RMB 11899 iPhone XS Max (64GB): RMB 9599 iPhone XS Max (256GB): RMB 10999 iPhone XS Max (512GB): RMB 12799 ''' -
函數(shù)的定義:
def func1(): print('Hello from function 1.') def func2(par): print('Hello from function 2 who accepts an argument {}'.format(par)) def func3(par='DEFAULT'): print('Hello from function 3 who ' 'accepts an argument {} if given and offers ' 'a default argument.'.format(par)) def func4(par1, par2='DEFAULT'): print('Hello from function 4 who ' 'accepts a first argument {} and offers ' 'a second default argument {} if not given.'.format(par1,par2)) func1() func2('Potato') func3() func3('Tomato') func4('Mango') func4('Mosquito','Hero') ''' Hello from function 1. Hello from function 2 who accepts an argument Potato Hello from function 3 who accepts an argument DEFAULT if given and offers a default argument. Hello from function 3 who accepts an argument Tomato if given and offers a default argument. Hello from function 4 who accepts a first argument Mango and offers a second default argument DEFAULT if not given. Hello from function 4 who accepts a first argument Mosquito and offers a second default argument Hero if not given. ''' -
Python 文件讀寫常常用
with ... as ...的語句:with open('notAFile.csv', 'rw', encoding='utf-8') as f: #這個(gè)時(shí)候文件已經(jīng)被成功打開,文件的使用權(quán)交給了f這個(gè)對(duì)象 content.list = f.readlines() #這個(gè)時(shí)候文件被成功地關(guān)閉,文件的使用權(quán)被釋放,f不再操縱這個(gè)文件從我上面的解析中你也能看懂,往常的 C++ 的寫法是用 fstream 來操作文件,在文件讀取之前要“打開”這個(gè)文件,在文件操作完畢之后還一定要“關(guān)閉”這個(gè)文件。而在 Python 中,你會(huì)發(fā)現(xiàn)沒有任何
open和close函數(shù),而是采用with ... as ...得到的句柄來代替我們完成這件事情。這樣?jì)寢屧僖膊挥脫?dān)心我忘記寫close函數(shù)啦!
Python 3 語法筆記
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。