計算機能夠進行的運算有不少種,運算按種類可分爲算數運算、比較運算、邏輯運算、賦值運算、成員運算、身份運算。字符串和列表的算數運算只能用+和*,字典沒有順序,因此不能進行算數運算和比較運算。比較運算中==比較的是值,is比較的是id。比較運算只能在同種類型下進行比較。字符串的比較是按照順序依次進行比較。邏輯運算的順序前後爲要用括號來表示。python
算術運算git
如下假設a=10,b=20api
比較運算bash
如下假設a=10,b=20app
賦值運算iphone
邏輯運算ide
成員運算spa
身份運算code
內置方法就是python中已經寫好的方法,咱們不用管原理直接拿來用就行。因此內置方法是規定好的,咱們想要學會就必須是所有記住。orm
字符串的內置方法包括:移除空白strip、切分split、長度len、切片(切出子字符串)、startswith和endswith、替代replace、查找find(顧頭不顧尾,找不到則返回-1不報錯)、index(顧頭不顧尾,但找不到會報錯)、count(顧頭不顧尾,若不指定範圍則查找全部)、格式化輸出%或.format()、插入join、插入空格expandtabs、全大寫upper和全小寫lower、首字母大寫capitlize、大小寫翻轉swapcase、每一個單詞的首字母大寫title、插入符號
移除空白strip:
msg=' hello ' print(msg) print(msg.strip())#hello msg='***hello*********' msg=msg.strip('*') print(msg)#hello print(msg.lstrip('*'))#hello********* print(msg.rstrip('*'))#***hello
舉個栗子(程序交互,若是用戶在輸入的用戶名或者密碼時不當心多按了空格,也不會形成錯誤,能夠正常登錄)
while True: name=input('user: ').strip() password=input('password: ').strip() if name == 'egon' and password == '123': print('login successfull')
切分split:
info='root:x:0:0::/root:/bin/bash' print(info[0]+info[1]+info[2]+info[3])#root user_l=info.split(':') print(user_l[1])#x msg='hello world egon say hahah' print(msg.split()) #['hello', 'world', 'egon', 'say', 'hahah'] #默認以空格做爲分隔符 cmd='download|xhp.mov|3000' cmd_l=cmd.split('|') print(cmd_l[1])#xhp.mov print(cmd_l[0])#downland print(cmd.split('|',1))#['download', 'xhp.mov|3000']
長度len:
print(len('hell 123'))
切片(切出子字符串):
msg='hello world' print(msg[1:3]) #el print(msg[1:4]) #ell
startswith和endswith:
name='you_suck' print(name.endswith('uk'))#True print(name.startswith('y')#True print(name.startswith('w')#False
替代replace:
name='jack say :i have a iphone,my name is jack' print(name.replace('jack','john',1)) #john say :i have a iphone,my name is jack
查找find(顧頭不顧尾,找不到則返回-1不報錯)
index(顧頭不顧尾,但找不到會報錯)
count(顧頭不顧尾,若不指定範圍則查找全部):
name='jack say hello' print(name.find('S',1,3)) #顧頭不顧尾,找不到則返回-1不會報錯,找到了則顯示索引 print(name.index('S')) #同上,可是找不到會報錯 print(name.count('S',1,5)) #顧頭不顧尾,若是不指定範圍則查找全部
格式化輸出%或.format():格式化輸出宏須要用到佔位符,通常統一使用%s
print('my name is %s my age is %s my sex is %s' %('jack',18,'male')) #my name is jack my age is 18 my sex is male print('my name is {} my age is {} my sex is {}'.format('jack',18,'male')) #my name is jack my age is 18 my sex is male print('my name is {0} my age is {1} my sex is {0}:{2}'.format('jack',18,'male')) #my name is jack my age is 18 my sex is egon:male print('my name is {name} my age is {age} my sex is {sex}'.format( sex='male', age=18, name='jack')) #my name is jack my age is 18 my sex is male
插入join(切片split的反方向):
info='root:x:0:0::/root:/bin/bash' print(info.split(':'))#['root', 'x', '0', '0', '', '/root', '/bin/bash'] l=['root', 'x', '0', '0', '', '/root', '/bin/bash'] print(':'.join(l))#root:x:0:0::/root:/bin/bash
插入空格expandtabs:
name='jack\thello' print(name))#jack hello#即倆單詞中間插入了一個縮進的長度的空格 print(name.expandtabs(1)#jack hello#即倆單詞中間插入了一個長度的空格
全大寫upper和全小寫lower:
name='jAck' print(name.lower()) print(name.upper())
首字母大寫capitlize、大小寫翻轉swapcase、每一個單詞的首字母大寫title:
name='jAck' print(name.capitalize()) #首字母大寫,其他部分小寫 print(name.swapcase()) #大小寫翻轉 msg='jack say good morning' print(msg.title()) #每一個單詞的首字母大寫
插入符號:
name='jack' print(name.center(30,'-'))#-------------jack------------- print(name.ljust(30,'*'))#jack************************** print(name.rjust(30,'*'))#**************************jack print(name.zfill(50)) #用0填充 #0000000000000000000000000000000000000000000000jack
在python3中
num0='4'
num1=b'4' bytes類型
num2=u'4' unicode,python3中無需加u就是unicode
num3='四' 中文數字
num4='Ⅳ' 羅馬數字
isdigt:str,bytes,unicode print(num0.isdigit()) print(num1.isdigit()) print(num2.isdigit()) print(num3.isdigit()) print(num4.isdigit()) isdecimal:str,unicode num0='4' num1=b'4' #bytes num2=u'4' #unicode,python3中無需加u就是unicode num3='四' #中文數字 num4='Ⅳ' #羅馬數字 print(num0.isdecimal()) # print(num1.) print(num2.isdecimal()) print(num3.isdecimal()) print(num4.isdecimal()) isnumeric:str,unicode,中文,羅馬 num0='4' num1=b'4' #bytes num2=u'4' #unicode,python3中無需加u就是unicode num3='四' #中文數字 num4='Ⅳ' #羅馬數字 print(num0.isnumeric()) # print(num1) print(num2.isnumeric()) print(num3.isnumeric()) print(num4.isnumeric())
列表的內置方法主要有索引、切片、追加appand、刪除pop、長度len、包含in、插入insert、count、清除clear、複製copy、翻轉reverse、排序sort。
切片:
l=['a','b','c','d','e','f'] print(l[1:5])#['b', 'c', 'd', 'e'] print(l[1:5:2])#['b', 'd']#其中的2表示步距 print(l[2:5])#['c', 'd', 'e'] print(l[-1])#f
追加append:
hobbies=['play','eat','sleep','study'] hobbies.append('girls') print(hobbies)#['play', 'eat', 'sleep', 'study', 'girls']
刪除pop:
hobbies=['play','eat','sleep','study'] x=hobbies.pop(1) #不是單純的刪除,是刪除而且把刪除的元素返回,咱們能夠用一個變量名去接收該返回值 print(x)#eat print(hobbies)#['play', 'sleep', 'study'] del hobbies[1] #單純的刪除 hobbies.remove('eat') #單純的刪除,而且是指定元素去刪除
就append和pop補充一個隊列和堆棧的小題目
#隊列:先進先出 queue_l=[] #入隊 queue_l.append('first') queue_l.append('second') queue_l.append('third') print(queue_l)#['first', 'second', 'third'] # 出隊 print(queue_l.pop(0))#first print(queue_l.pop(0))#second print(queue_l.pop(0))#third #堆棧:先進後出,後進先出 l=[] #入棧 l.append('first') l.append('second') l.append('third') print(l)#['first', 'second', 'third'] #出棧 print(l.pop())#third print(l.pop())#second print(l.pop())#first
長度len:
hobbies=['play','eat','sleep','study'] print(len(hobbies))#4
包含in:
hobbies=['play','eat','sleep','study'] print('sleep' in hobbies)#True msg='hello world jack' print('jack' in msg)#True
插入insert:
hobbies=['play','eat','sleep','study','eat','eat'] hobbies.insert(1,'walk') hobbies.insert(1,['walk1','walk2','walk3']) print(hobbies) #['play', ['walk1', 'walk2', 'walk3'], 'walk', 'eat', 'sleep', 'study', 'eat', 'eat']
另外一種插入extend:
hobbies=['play','eat','sleep','study','eat','eat'] hobbies.extend(['walk1','walk2','walk3']) print(hobbies)#['play', 'eat', 'sleep', 'study', 'eat', 'eat', 'walk1', 'walk2', 'walk3'] #注意extend與insert的區別
索引count、index:
hobbies=['play','eat','sleep','study','eat','eat'] print(hobbies.count('eat'))#3,個數 print(hobbies.index('sleep'))#2,下標位置
清除clear和複製copy簡單,不作詳述。
#存/取: info_dic={'name':'egon','age':18,'sex':'male'} print(info_dic['name11111111']) print(info_dic.get('name',None)) #刪除: info_dic={'name':'egon','age':18,'sex':'male'} info_dic.pop() info_dic.popitem() del info_dic['name'] #pop:key存在則彈出值,不存在則返回默認值,若是沒有默認值則報錯 print(info_dic.pop('nam123123123123123123e',None)) print(info_dic) print(info_dic.popitem()) print(info_dic) #鍵s,值s,鍵值對: info_dic={'name':'egon','age':18,'sex':'male'} print(info_dic.keys())#鍵 print(info_dic.values())#值 print(info_dic.items())#鍵值對 for k in info_dic:#循環輸出鍵值對 print(k,info_dic[k]) #長度len,比較簡單不作說明 #包含in,也較簡單不作說明 #升級:若原字典有則替換,若沒有則添加 info_dic={'name':'jack','age':18,'sex':'male'} info_dic.update({'a':1,'name':'jAck'}) print(info_dic) #增長: info_dic={'name':'jack','age':18,'sex':'male'} # info_dic['hobbies']=[] # info_dic['hobbies'].append('study') # info_dic['hobbies'].append('read') # print(info_dic) #copy和clear較簡單不作說明 #fromkeys:做用只是新建了一個字典,與原字典無關 d=info_dic.fromkeys(('name','age','sex'),None) print(d) d1=dict.fromkeys(('name','age','sex'),None) d2=dict.fromkeys(('name','age','sex'),('jack',18,'male')) print(d1) print(d2)
元祖的內置方法較少,主要爲切片、in、長度len、索引index、count。
切片:
goods=('iphone','lenovo','sanxing','suoyi') print(goods[1:3])#('lenovo', 'sanxing')
包含in:
goods=('iphone','lenovo','mi','zuk') print('iphone' in goods)#True d={'a':1,'b':2,'c':3} print('b' in d)#True
長度len:
hobbies=('play','eat','sleep','study') print(len(hobbies))#4
索引index、count:
goods=('iphone','lenovo','mi','zuk') print(goods.index('iphone'))#0,下標位置 print(goods.count('iphone'))#1,個數
in和not in
s={'a','b','c','d'} print('a'in s)#True
並集|
s_1={'a','b','c'} s_2={'a','b','d'} print(s_1|s_2)#{'a','b','c','d'} #也能夠用union print(s_1.union(s_2))
交集&
s_1={'a','b','c'} s_2={'a','b','d'} print(s_1&s_2)#{'a','b'} #也能夠用intersection print(s_1.intersection(s_2))
差集-
s_1={'a','b','c'} s_2={'a','b','d'} print(s_1-s_2)#{'c'} #也能夠用difference print(s_1.difference(s_2)) #{'c'} print(s_2.difference(s_1)) #{'d'}
對稱差集^:即二者的並集去掉二者的交集
s_1={'a','b','c'} s_2={'a','b','d'} print(s_1^s_2) #{'c','d'} #也能夠用symmetric_difference print(s_1.symmetric_difference(s_2))
父集、子集
set1={1,2,3,4,5} set2={1,2,3,4} print(set1 >= set2) #也能夠用issuperset print(set1.issupissubseterset(set2)) print(set2 <= set1) #也能夠用issubset print(set2.issubset(set1))
簡單要點,不作詳述:添加add、刪除pop(隨機刪除,由於集合是無序的)、remove(指定刪除,找不到則報錯)、discards(指定刪除,找不到不報錯)、升級update、清除clear、複製copy、解壓a*_。