python編程實(shí)例
01-Hello World
01-Hello World
python 的語(yǔ)法邏輯完全靠縮進(jìn),建議縮進(jìn)4個(gè)空格
如果是頂級(jí)代碼,必須頂格書(shū)寫(xiě),哪怕只有一個(gè)空格也會(huì)語(yǔ)法錯(cuò)誤
下面實(shí)例中,滿(mǎn)足if條件要輸出兩行內(nèi)容,這兩行內(nèi)容必須都縮進(jìn),而且具有相同的縮進(jìn)級(jí)別。
print("hello world") if 3 > 0: print("OK") print("yes") x = 3; y = 4 #不推薦,還是應(yīng)該寫(xiě)成兩行 print(x + y)
02-print
print("hello world") #hello world print("hello","world") #逗號(hào)自動(dòng)添加默認(rèn)的分隔符 hello world print("hello" + "world") #加號(hào)表示字符拼接 helloworld print("hello","world",sep="***") #單詞間用***分隔 hello***world print("*" * 10) #*號(hào)重復(fù)10遍 ********** print("how are you?",end="") #默認(rèn)print會(huì)打印回車(chē),end=""表示不要回車(chē)
03-基本運(yùn)算
運(yùn)算符可以分為:算術(shù)運(yùn)算符、比較運(yùn)算符和邏輯運(yùn)算符。優(yōu)先級(jí)是:算術(shù)運(yùn)算符>比較運(yùn)算符>邏輯運(yùn)算符。不過(guò)呢,沒(méi)記住優(yōu)先級(jí),最好使用括號(hào),這樣不用背,也增加了代碼的可讀性。
print(5 / 2) #2.5 print(5 // 2) #丟棄余數(shù),只保留商 2 print(5 % 2) #求余數(shù) 1 print(5 ** 3) #求5的3次方 125 print(5 > 3)#返回True print(3 > 5)#返回False print(20 > 10 >5) #python支持連續(xù)比較 True print(20 >10 and 10 >5) #python支持連續(xù)比較,與上面相同含義 True print(not 20 > 10) #False print(not 10 > 20) #True
True和False是關(guān)鍵字,區(qū)分大小寫(xiě)
04-input
number = input("請(qǐng)輸入數(shù)字:") #input用于獲取鍵盤(pán)輸入 20 print(number) # 20 print(type(number)) #input獲得的數(shù)據(jù)是字符型#print(number + 10) #報(bào)錯(cuò),不能把字符和數(shù)字做運(yùn)算 TypeError: must be str, not int print(int(number) + 10) #int可將字符10轉(zhuǎn)換成數(shù)字10 30 print(number + str(10)) #str可將10轉(zhuǎn)換為字符串后實(shí)現(xiàn)字符串拼接 2010
05-輸入輸出基礎(chǔ)練習(xí)
username = input("username: ") #劉備 print("welcome",username) #print各項(xiàng)間默認(rèn)以空格作為分隔符 welcome 劉備 逗號(hào)默認(rèn)中間有一個(gè)空格 print("welcome " + username) #注意引號(hào)內(nèi)最后的空格 welcome 劉備 第一個(gè)字符串中包含兩個(gè)空格
06-字符串使用基礎(chǔ)
python中,單雙引號(hào)在字符串中使用,沒(méi)有區(qū)別
sentence = 'tom\'s pet is a cat' #單引號(hào)中間還有單引號(hào),可以轉(zhuǎn)義 sentence2 = "tom's pet is a cat" #也可以用雙引號(hào)包含單引號(hào) sentence3 = "tom said:\"hello world!\"" sentence4 = 'tom said:"hello world!"' #單引號(hào)中間可以用雙引號(hào),可以轉(zhuǎn)義 #三個(gè)連續(xù)的三引號(hào)或單引號(hào),可以保存輸入格式,允許輸入多行字符串 words = """ hello world abcd""" print(words) #hello #world #abcd py_str = 'python' print(len(py_str)) #取長(zhǎng)度 6 print(py_str[0]) #取第一個(gè)字符 p print('python'[0]) #取第一個(gè)字符 p print(py_str[-1]) #取最后一個(gè)字符 n #print(py_str[6]) #錯(cuò)誤,下表超出范圍 IndexError: string index out of range print(py_str[2:4]) #切片,起始下標(biāo)包含,結(jié)束下標(biāo)不包含 th print(py_str[2:]) #從下標(biāo)為2的字符渠道結(jié)尾 thon print(py_str[:2]) #從開(kāi)頭取到下標(biāo)為2之前的字符 py print(py_str[:]) #取全部 python print(py_str[::2]) #按照步長(zhǎng)為2取值,默認(rèn)為1 pto print(py_str[1::2]) #以下標(biāo)為1開(kāi)始,步長(zhǎng)為2取值 yhn print(py_str[::-1]) #步長(zhǎng)為負(fù),表示從右往左取 nohtyp print(py_str + 'is good') #簡(jiǎn)單拼接在一起 pythonis good print(py_str * 3) #把字符串重復(fù)3遍 pythonpythonpython print('t' in py_str) #True print('th' in py_str) #True print('to' in py_str) #False 子字符串必須連續(xù) print('to' not in py_str) #True
07-列表基礎(chǔ)
列表也是序列對(duì)象,但它是容器類(lèi)型,列表中可以包含各種數(shù)據(jù),列表可變
alist = [10,20,30,'bob','alice',[1,2,3]] print(len(alist)) #6 print(alist[-1]) #取出最后一項(xiàng) [1, 2, 3] print(alist[-1][-1]) #因?yàn)樽詈笠豁?xiàng)是列表,還可以繼續(xù)取下標(biāo) 3 print(alist[-2][2]) #列表倒數(shù)第二項(xiàng)是字符串,還可以繼續(xù)取下標(biāo) i print(alist[3:5]) #['bob','alice'] print(10 in alist) #True print('o' in alist) #False print(100 not in alist) #True alist[-1] = 100 #修改最后一項(xiàng)的值 print(alist) # [10, 20, 30, 'bob', 'alice', 100] alist.append(200) #向列表中追加一項(xiàng) print(alist) #[10, 20, 30, 'bob', 'alice', 100, 200]
08-元組基礎(chǔ)
元組與列表基本上是一致的,只是元組不可變,列表可變
atuple = (10,20,30,'bob','alice',[1,2,3]) print(len(atuple)) #6 print(atuple[2]) #30 print(atuple[-1]) #[1, 2, 3] print(atuple[3:5]) #('bob','alice') print(10 in atuple) #True atuple[-1] = 100 #錯(cuò)誤,元組是不可變的 TypeError: 'tuple' object does not support item assignment