圖書推薦:《Python風格指南》git
1.字母數字下劃線組成api
2.不能以數字開頭,不能含有特殊字符和空格app
3.不能以保留字命名ide
4.不能以中文命名函數
5.定義的變量名應該有意義spa
6.駝峯式命名、下劃線分割單詞orm
7.變量名區分大小寫排序
1.if <表達式> :索引
elif <表達式> :ip
else :
2.while <表達式> :
3.for i in range(循環次數):
4.break跳出當前循環,若是有兩層,只能跳出內層
5.continue結束本次循環,開始下一次循環的條件判斷
6.條件表達式末尾要加「:」,while、if、else、elif、for都是如此,但break和continue不用
1.# 單行註釋
2.'''多行註釋''' (三個引號賦值給變量,可按引號內內容原樣打印,多用於打印多行,如msg='''內容''',print(msg))
3.""" 多行註釋 """
4.註釋快捷鍵Ctrl+/
and or not 遵循短路原則
%s s = string
%d d = digit 整數
%f f = float 浮點數,約等於小數
1.列表用[],元組用()
2.查:
(1)索引(下標) ,都是從0開始 a[2]
(2)切片 a[起始:結束(不包含該位置元素):步長]
(3)count 查某個元素的出現次數
(4).index 根據內容找其對應的位置
3.增:
(1)a.append() 追加
(2)a.insert(index, "內容")
(3)a.extend 擴展
4.改:
(1)a[index] = "新值"
(2)a[start:end] = [a,b,c]
5.刪:
(1)remove("內容")
(2).pop(index) 有返回值
(3)del a a已經不存在 del a[index] 刪一個元素
(4)a.clear() 清空,可是a還在
6.排序:
sort ()
reverse()
.sort(reverse=Ture) 從大到小排列
.sort() 從小到大排列
1.字典用{},無序,鍵惟一
2.格式:dic={‘鍵’:‘值’,‘鍵’:‘值’}
3.查:
經過鍵查找
4.增:
.setdefault(‘鍵’,‘值’) 鍵存在,不改動,返回字典中相應的鍵對應的值;鍵不存在,在字典中中增長新的鍵值對,並返回相應的值
dic['鍵']=值
5.改:
old.update(new) 若是old中有new的鍵,更新,不然建立新的鍵並補充值
dic['鍵']=新值
6.刪:
(1).clear() 清空字典
(2)del dic['鍵'] 刪除字典中指定鍵值對
(3).pop('鍵') 刪除字典中指定鍵值對,並返回該鍵值對的值
(4)del dic 刪除整個字典
7.初始化:
dic.fromkeys(['host1','host2','host3'],'test')
8.遍歷:
for i in dic:
print(i,dic[i])
1.重複輸出字符串
print('內容' * 重複次數)
2. [] ,[:] 經過索引獲取字符串中字符,這裏和列表的切片操做是相同的,具體內容見列表
print('helloworld'[2:])
3.關鍵字 in 返回True或者False
print(123 in [23,45,123])
print('el' in 'hello')
4.格式字符串
print('zhou is a good teacher')
print('%s is a good teacher'%'zhou')
5.字符串拼接
a='123'
b='abc'
d='44'
c=a+b+d
c= '分隔內容'.join([a,b,d])
6.關於轉義
要想顯示字符串'he's good',應寫爲'he\'s good'
要想顯示\n而不讓其變爲換行符,則寫爲repr(\n)
7.字符串內置函數
(1)print(st.count('元素')) # 統計元素個數
(2)print(st.capitalize()) # 字符串首字母大寫
(3)print(st.center(總字符數,'填充符號')) # 居中
(4)print(st.endswith('字符串')) # 判斷是否以某個內容結尾
(5)print(st.startswith('字符串')) # 判斷是否以某個內容開頭,在文件處理中常用
(6)print(st.expandtabs(tabsize=個數)) #自定義TAB表明空格數
(7)print(st.find('字符串')) # 查找到第一個元素,並將索引值返回,找不到返回-1
(8)print(st.format(name='zhou',age=20)) # 格式化輸出的另外一種方式,字符串中要替換的內容在{}裏面
(9)print(st.format_map({'name':'zhou','age':20})) #和上面一種方法的區別是參數是一個字典
(10)print(st.index('t')) #同find同樣,可是找不到報錯
(11)print('asd'.isalnum()) #判斷是否是字母或字符串
(12)print('12632178'.isdecimal()) #判斷是否是十進制
(13)print('1269999.uuuu'.isnumeric()) #判斷是否是整數
(14)print('abc'.isidentifier()) #是否是變量名
(15)print('Abc'.islower()) #是否是小寫
(16)print('ABC'.isupper()) #是否是大寫
(17)print(' e'.isspace()) #是否是空格
(18)print('My Title'.istitle()) #是否是每一個單詞首字母大寫
(19)print('My title'.lower()) #全部大寫變小寫
(20)print('My title'.upper()) #全部小寫變大寫
(21)print('My title'.swapcase()) #大寫變小寫,小寫變大寫
(22)print('My title'.ljust(字符串總數,'填充符號')) #左對齊
(23)print('My title'.rjust(字符串總數,'填充符號')) #右對齊
(24)print('\tMy title\n'.strip()) #去掉空格和換行符
(25)print('\tMy title\n'.lstrip()) #去掉左邊空格和換行符
(26)print('\tMy title\n'.rstrip()) #去掉右邊空格和換行符
(27)print('ok')
(28)print('My title title'.replace('被替換字符串','替換內容',替換次數)) #替換
(29)print('My title title'.rfind('查找內容')) #從右往左找字符串
(30)print('My title title'.split('分割符',分割次數)) #分割
(31)print('My title title'.title()) #改成標題格式