Python的數據類型
I數值
1:變量先聲明
2:表達式
>>> 2.2+3.0 5.2
3:顯示
>>> '{0}'.format(20)#20為站位傳遞符 '20' >>> f=3.33333333 >>> 'f={0:.2f}'.format(f)#0表示第一個站位符,2f表示保留2個小數點 'f=3.33'
4:比較:>、<、>=、<=、==、!=
5:相除
>>> 10//4.0 #//是取整,結果是按精度高的操作,地板除 2.0
6:取整
math.floor:往左
math.trunc往0
round四舍五入
>>> import math >>> math.floor(-3.4) -4 >>> math.trunc(-3.94) -3 >>> round(-3.14) -3
7整型
>>> oct(64)#轉換8進制 '0o100' >>> hex(64)#轉換16進制 '0x40' >>> bin(64)#轉換2進制 '0b1000000'
8:進制:0o8進制、0x16進制、0b2進制、字面值
注:008沒有
9:Decimal做精確運算,是個數學模塊
>>> import decimal#導入模塊 >>> decimal.dacimal('3.14') >>> decimal.Decimal('3.14') Decimal('3.14') >>> decimal.Decimal('1.1')+decimal.Decimal('2.2') Decimal('3.3')
II字符串
1聲明:' '、" "、"""##""
>>> name='tom' >>> word="what's your name" >>> word "what's your name" >>> 'what\'s your name'#\'s轉譯 "what's your name"
2轉譯:\、\'、\''、\n、\b、\t(table4個空格)、\a
3忽略轉義符:r'.......'
>>> path =r'D:\PycharmProjects\result.txt'#r:原始表面為row >>> path ='D:\\PycharmProjects\\result.txt'#另一種表示方法
4基本操作
(1)引號:有單引號、雙引號 如:"""計算平均分""""這個不會忽略會生成文檔
>>> s = 'hello' >>> for c in s: print(c) h e l l o >>> '\n' '\n' >>> for c in s: print(c,end= '') hello
s[lon(s)-1]#取最后一個
s[::2]#每個一個取
h[::-1]#從右往左
ord('c')#看c的順序
chr(99)#把99傳進去
(2)布爾型:主要是判斷True和False
True == 1#==判斷
False == 0
(3)替換:.replace()
有個關於列表等於前面字符串,后替換的操作
(4)切割:.split()
>>> s ='codeclassroom.com' >>> l=list(s) >>> l ['c', 'o', 'd', 'e', 'c', 'l', 'a', 's', 's', 'r', 'o', 'o', 'm', '.', 'c', 'o', 'm']>>> l[len(l)-1] = 'c' >>> l ['c', 'o', 'd', 'e', 'c', 'l', 'a', 's', 's', 'r', 'o', 'o', 'm', '.', 'c', 'o', 'c'] >>> l[-1]='n' >>> l ['c', 'o', 'd', 'e', 'c', 'l', 'a', 's', 's', 'r', 'o', 'o', 'm', '.', 'c', 'o', 'n']>>> s = ''.join(l) >>> s 'codeclassroom.con' >>> s =','.join(l) >>> s 'c,o,d,e,c,l,a,s,s,r,o,o,m,.,c,o,n' >>> url = 'uke.cc' >>> url.startswith('http://') False >>> url = 'coderoom.com,uke.cc,youketang.com' >>> url.split(',')#切割 ['coderoom.com', 'uke.cc', 'youketang.com'] >>> l= url.split(',') >>> l ['coderoom.com', 'uke.cc', 'youketang.com']
(5)判斷:.starswith以什么開頭,.endwith以什么結尾,.find在什么位置
>>> url = 'uke.cc' >>> url.startswith('http://') False >>> url.find('uke') 0 >>> url.find('u') 0
(6)格式化字符串:.format()
>>> a=1 >>> b=2 >>> a,b = 1,2 >>> a 1 >>> b 2 >>> a,b = b,a >>> a 2 >>> b 1 >>> '{0} => {1}'.format(a,b)#format是傳遞值 '2 => 1'
>>> '{name}=>{salary}'.format(name='tome',salary=9000.00)
'tome=>9000.0'
III列表
常用的操作有:.append() .extend() .sort() .reverse() .index() .count()
>>> len([1,2,3]) 3 >>> l = list('youpinketang') >>> l ['y', 'o', 'u', 'p', 'i', 'n', 'k', 'e', 't', 'a', 'n', 'g'] >>> 'k'in l True >>> for c in l:#把列表中所有元素找出放入c print(c)#print默認加\n y o u p i n k e t a n g >>> for c in l:print (c,end=',') y,o,u,p,i,n,k,e,t,a,n,g,
append()
>>> l = [1,2,3,6,9]#計算平方放入新列表 >>> res = []#res結果,[]申空列表 >>> for i in l: res.append(i**2)#append追加 >>> res [1, 4, 9, 36, 81] >>> l [1, 2, 3, 6, 9] >>> l1 = [i**2 for i in l] >>> l1 [1, 4, 9, 36, 81] >>> [c*3 for c in 'code']#cede轉列每一個拿出來乘3 ['ccc', 'ooo', 'ddd', 'eee'] >>> l.append(7)#append追加,list支持原位改變,str不支持 >>> l [1, 2, 3, 6, 9, 7]
extend()
>>> l.extend([9,8,5])#append追加1個元素,extend擴展列表 >>> l [1, 2, 3, 6, 9, 7, 9, 8, 5]
sort()排序小到大,reverse()反着排
>>> l [1, 2, 3, 6, 9, 7, 9, 8, 5] >>> l.sort() >>> l [1, 2, 3, 5, 6, 7, 8, 9, 9] >>> l.reverse() >>> l [9, 9, 8, 7, 6, 5, 3, 2, 1]
pop()彈出
>>> l [9, 9, 8, 7, 6, 5, 3, 2, 1] >>> l.pop()#彈出 1 >>> l.pop() 2 >>> l.pop() 3
del()
>>> del(l[0]) >>> l [9, 8, 7, 6, 5]
index()
>>> l.index(7)
2
count()統計
>>> l.count(5)
1
多個變量指向同一個對象
>>> l1 = [1,2,3,4,5] >>> l2 = l1#l1,l2指同1個對象 >>> l2 [1, 2, 3, 4, 5] >>> l1[1]#第二個位置替換成9 2 >>> l1[1] = 9#第二個位置替換成9 SyntaxError: unexpected indent >>> l1[1] = 9#第二個位置替換成9 >>> l1 [1, 9, 3, 4, 5]
不想多個變量指向同一個對象,兩種方法
>>> l1#法1 [1, 9, 3, 4, 5] >>> l3 = l1[:]#把l1拿出來傳給l3 >>> l3 [1, 9, 3, 4, 5] >>> l1[2]=100#第三個元素換成100 >>> l1 [1, 9, 100, 4, 5] >>> l4 = l1.copy()#法2copy制造副本 >>> l4 [1, 9, 100, 4, 5]