python編程實(shí)例
01-Hello World
01-Hello World
python 的語法邏輯完全靠縮進(jìn),建議縮進(jìn)4個空格
如果是頂級代碼,必須頂格書寫,哪怕只有一個空格也會語法錯誤
下面實(shí)例中,滿足if條件要輸出兩行內(nèi)容,這兩行內(nèi)容必須都縮進(jìn),而且具有相同的縮進(jìn)級別。
print("hello world") if 3 > 0: print("OK") print("yes") x = 3; y = 4 #不推薦,還是應(yīng)該寫成兩行 print(x + y)
02-print
print("hello world") #hello world print("hello","world") #逗號自動添加默認(rèn)的分隔符 hello world print("hello" + "world") #加號表示字符拼接 helloworld print("hello","world",sep="***") #單詞間用***分隔 hello***world print("*" * 10) #*號重復(fù)10遍 ********** print("how are you?",end="") #默認(rèn)print會打印回車,end=""表示不要回車
03-基本運(yùn)算
運(yùn)算符可以分為:算術(shù)運(yùn)算符、比較運(yùn)算符和邏輯運(yùn)算符。優(yōu)先級是:算術(shù)運(yùn)算符>比較運(yùn)算符>邏輯運(yùn)算符。不過呢,沒記住優(yōu)先級,最好使用括號,這樣不用背,也增加了代碼的可讀性。
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ū)分大小寫
04-input
number = input("請輸入數(shù)字:") #input用于獲取鍵盤輸入 20 print(number) # 20 print(type(number)) #input獲得的數(shù)據(jù)是字符型#print(number + 10) #報(bào)錯,不能把字符和數(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 劉備 逗號默認(rèn)中間有一個空格 print("welcome " + username) #注意引號內(nèi)最后的空格 welcome 劉備 第一個字符串中包含兩個空格
06-字符串使用基礎(chǔ)
python中,單雙引號在字符串中使用,沒有區(qū)別
sentence = 'tom\'s pet is a cat' #單引號中間還有單引號,可以轉(zhuǎn)義 sentence2 = "tom's pet is a cat" #也可以用雙引號包含單引號 sentence3 = "tom said:\"hello world!\"" sentence4 = 'tom said:"hello world!"' #單引號中間可以用雙引號,可以轉(zhuǎn)義 #三個連續(xù)的三引號或單引號,可以保存輸入格式,允許輸入多行字符串 words = """ hello world abcd""" print(words) #hello #world #abcd py_str = 'python' print(len(py_str)) #取長度 6 print(py_str[0]) #取第一個字符 p print('python'[0]) #取第一個字符 p print(py_str[-1]) #取最后一個字符 n #print(py_str[6]) #錯誤,下表超出范圍 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]) #從開頭取到下標(biāo)為2之前的字符 py print(py_str[:]) #取全部 python print(py_str[::2]) #按照步長為2取值,默認(rèn)為1 pto print(py_str[1::2]) #以下標(biāo)為1開始,步長為2取值 yhn print(py_str[::-1]) #步長為負(fù),表示從右往左取 nohtyp print(py_str + 'is good') #簡單拼接在一起 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ǔ)
列表也是序列對象,但它是容器類型,列表中可以包含各種數(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 #錯誤,元組是不可變的 TypeError: 'tuple' object does not support item assignment