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