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]