1.注釋
1 # 單行注釋 這是單行注釋 2 '''多行注釋''' 這是多行注釋 3 """多行注釋""" 這是多行注釋
2.識別符號
我們自己在寫代碼的時候取的名字,命名的符號
1 專案名 --->project name 2 包名 --->package name 3 模塊名 ---> .py python檔案名
2.1.規范
1 A.由字母/數字下劃線組成,但是不能以數字開頭 2 B.見名知意 3 C.不同的字母/數字之間用下劃線隔開,提升可讀性 4 D.不能用關鍵字
3.變數名
1 # 變數名 = 變數值 2 a=2 #右邊的值賦值給左邊的變數 3 print(a) #prin(你要輸出的內容) 輸入出函式:輸出內容到控制臺
4.資料型別
1 #整數型: 關鍵字 int 2 a=10 3 print(a+10) 4 5 #浮點型:關鍵字 float 6 b=10.0 7 8 #布爾型:關鍵字 bool True False 首寫字母要大寫 9 print(True) 10 11 #字串: 關鍵字str 成對的單引號/雙引號/三引號 12 c='hello' 13 d="hello" 14 e='''hello''' 15 f="""hello""" 16 17 #type(資料) 獲取資料型別 int float str bool 18 print(type(d)) #輸出<class 'str'>
4.1.字串
#字串里面的元素:單個字母,數字,漢字,單個符號都稱為一個元素
#len(資料)統計資料的長度(print(lent(f)))
#字串取值:字串名[索引值]
#索引:從0開始標記
s = "hello"
| h | e | l | l | o | 字串 |
| 0 | 1 | 2 | 3 | 4 | 正序索引 |
| -5 | -4 | -3 | -2 | -1 | 反序索引 |
print(s[0]) #輸出為 h print(s[2]) #輸出為 l print(s[-1])#輸出為 o print(s[-4])#輸出為 e
4.1.1.切片
1 #字串取多個值:切片,字串名[索引頭:索引尾:尾長] 步長默認為1 2 f = "hello word!" 3 print(f[1:5:1])#取頭不取尾 與print(f[1:5])一致 結果:ello 4 print(f[1:5:2])#結果:el 5 print(f[2:4:2])#結果:l 6 print(f[:])#全部列印 7 print(f[:4])#0123 8 print(f[3:0])#3以后全部列印 9 10 #練習:請利用切片,倒序輸出s的值,輸出結果為:!drow 11 print(f[-1:-7:-1]) #-1 -2 -3 -4 -5 -6 -7
4.1.2.字串的分割 split()
1 # 字串.split(指定的切割符號,切割次數),回傳一個串列型別的資料, 2 # 串列里面的子元素都是字串型別 3 # 指定的切割符被切走不保留 4 s = " hello!" 5 print(s.split()) #輸出:[' hello'] 6 print(s.split("e")) #輸出:[' h','llo!'] 7 print(s.split('l')) #輸出:[' he','','ol'] 8 print(s.split("l",1)) #輸出[' he','lo!'] 只切割一次
4.1.3.字串的替換 replace()
1 # 字串.replace(指定替換值,新值,替換次數) 2 s = " hello!" 3 f = s.replace('e','x') 4 print(f)#輸出:"hxllo" 5 m = s.replace('l','x',2) 6 print(m)#輸出:"hexxo!"
4.1.4.字串的去除指定字符 strip()
1 #字串.strip(指定字符) 2 #默認去掉兩側空格,只能去掉頭和尾的指定字符 3 # 4 s = " hello!" 5 print(len(s))# 輸出: 8 6 f = s.strip() 7 print(f) # 輸出:hello 8 print(len(f))# 輸出:6 去除兩側空格 9 m = s.strip("!") 10 print(m)# 輸出:hello
4.1.5.字串拼接
1 s_1 = "python11" 2 s_2 = "中秋節快樂" 3 s_3 = 666#整數 str(數字)----->強制轉為str型別 4 print(s_1+s_2) #拼接兩個字串,輸出:python11中秋節快樂 5 print(s_1,s_2) #分別輸出兩個變數 ,輸出:python11 中秋節快樂 6 # print(s_1+s_2+s_3)#報錯 ERROR 7 print(s_1,s_2,s_3)#輸出:python11 中秋節快樂 666
4.1.6.字串格式化輸出 % format
1 #格式化輸出1:format 特點{} 2 nm1 = "字串1" 3 nm2 = 56.2 4 nm3 = 33 5 print("這是個{0}的{1},很{2}".format(nm1,nm2,nm3)) #輸出:這是個字串1的字串2,很33 6 #格式化輸出2:% %s字串 %d數字 %f浮點數0 7 # %s--->%d--->%f 型別覆寫 8 print("這是個%s的%f,很%d"%(nm1,nm2,nm3)) #輸出:這是個字串1的56.200000,很33 9 print("這是個%s的%s,很%s"%(nm1,nm2,nm3)) #輸出:這是個字串1的56.2,很33 10 print("這是個%d的%d"%(nm2,nm3)) #輸出:這是個字串1的56.2,很33
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/539919.html
標籤:其他
上一篇:Python3.7.3環境搭建
下一篇:day02-功能實作01

