python数据类型
数据类型
数字:int ,float,complex
布尔:True,False
字符串:'',"", '''....'''
列表 list:[] 带索引有序的,字符串操作的行为都适合它
元组 tuple:()
字典 dict:{'a':1}
集合 set:{'a','b'}
数字:
int ,float,Complex
>>> a=1 >>> type(a) <class 'int'> >>> p=3.141592653 >>> p 3.141592653 >>> type(p) <class 'float'> >>> c=3+4j >>> d=4+2j >>> c+d (7+6j) >>> type(c) <class 'complex'> >>> a+p 4.141592653 >>> a+c (4+4j) >>> p*100000000000000000 3.141592653e+17 >>> 0.23/1000000000000000 2.3e-16
bool(布尔值/逻辑值)
对与错、真与假、空与非空
只包含2个值
True:表示非空的对象(string,tuple,list,set,dictonary等以及所有非0数)
False:表示0,None空的对象等
作用:主要用于判断语句中
一个字符串是否是空的
一个运算结果是否为0
一个表达式是否可用
注意('')与[''],{''},{'':''}的区别
('')为假,[''],{''},{'':''}为真
bool()
n [82]: bool('') Out[82]: False In [83]: bool(' ') Out[83]: True In [84]: bool(0) Out[84]: False In [85]: bool('0') Out[85]: True In [89]: a=() In [90]: type(a) Out[90]: tuple In [91]: bool(a) Out[91]: False In [92]: a=('') In [93]: bool(a) Out[93]: False In [94]: a=(' ') In [95]: bool(a) Out[95]: True In [104]: bool([]) Out[104]: False In [105]: bool(['']) Out[105]: True In [113]: bool({}) Out[113]: False In [114]: bool({''}) Out[114]: True In [121]: a={'':''} In [122]: type(a) Out[122]: dict In [123]: bool(a) Out[123]: True
isinstance(object, classinfo)
如果参数object是classinfo的实例,或者object是classinfo类的子类的一个实例, 返回True
如果object不是一个给定类型的的对象,则返回结果总是False。
如果classinfo不表示一个类(类型对象),那么它要么是一个类的元组, 或者递归地包含这样的(由数据类型构成的)元组.其他的序列类型是不被允许的。
如果classinfo不是一种数据类型或者由数据类型构成的元组,将引发一个TypeError异常
len()
In [15]: type(1) Out[15]: int In [16]: isinstance(1,type(1)) Out[16]: True In [17]: isinstance(1.0,int) Out[17]: False In [18]: isinstance(1.0,float) Out[18]: True In [19]: isinstance((),tuple) Out[19]: True In [20]: isinstance({},dict) Out[20]: True In [21]: isinstance([],list) Out[21]: True In [22]: isinstance({''},set) Out[22]: True In [23]: isinstance({'':''},dict) Out[23]: True len() In [24]: len('a') Out[24]: 1 In [25]: len(['a','b']) Out[25]: 2 In [26]: len(['aa','b']) Out[26]: 2 In [27]: len((1,2,'a')) Out[27]: 3 In [28]: len({'a':'ajax','p':'php'}) Out[28]: 2 In [31]: len((1,2,'a',{'a':'ajax','b':[1,2,'aaa']})) Out[31]: 4
字符串:
1.单引号
'字符串'
2.双引号
"字符串"
3.三重单引号(类似注释)
,,,
大段数据
定义在三重引号(单或者双)里
术语叫docstring
,,,
In [1]: s='hello word' In [2]: s Out[2]: 'hello word' In [3]: type(s) Out[3]: str In [4]: say='tom say:"let\'s go"' In [5]: say Out[5]: 'tom say:"let\'s go"' In [6]: print(say) tom say:"let's go" say='tom say:let\'s go \n lily:ok' print(say) tom say:let's go lily:ok 如果不使用\n来保持换行 In [12]: say1=''' tom:let'go! ...: jim:wait me a moment ,I"will finish soon ...: tom:ok ...: ''' print(say1) tom:let'go! jim:wait me a moment ,I"will finish soon tom:ok In [15]: say1 Out[15]: ' tom:let\'go!\njim:wait me a moment ,I"will finish soon\ntom:ok\n'
特殊符号需要转义
定义原始字符串
使用
r "\ 没有意义但不能在结尾"
In [21]: str1=r"let's go\t ok" In [22]: str1 Out[22]: "let's go\\t ok" In [23]: str2="let's go \t ok" In [24]: str2 Out[24]: "let's go \t ok" In [25]: print(str1) let's go\t ok In [26]: print(str2) let's go ok
字符串的拼接
字符串+字符串,或者字符串*数字 ,不支持减法和数字的除法
In [28]: a='1' In [29]: b=2 In [30]: a+b --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-30-ca730b97bf8a> in <module>() ----> 1 a+b TypeError: can only concatenate str (not "int") to str In [31]: a+str(b) Out[31]: '12' In [32]: b+int(a) Out[32]: 3 In [33]: a*3 Out[33]: '111' In [34]: str(b)-a --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-34-df4abb960107> in <module>() ----> 1 str(b)-a TypeError: unsupported operand type(s) for -: 'str' and 'str'
字符串分片和索引
成员关系操作符
in,not in
In [40]: 'hello' in 'hello world' Out[40]: True lang="php js python" >>> if "php" in lang : ... print("php is ok") ... print("ok is php") ... php is ok ok is php >>>
字符串截取
String[i:j:k]
string[i]
string[i:j]
string[i:]
string[:j]
string[:]
i表示从第某个位置开始
j到第几个
k表示每隔几个取一个
对i,j前闭后开(到j但是不包含第j位置上的)
In [7]: s="hello world" In [8]: s[0] Out[8]: 'h' In [9]: s[-1] Out[9]: 'd' In [10]: s[0:5] Out[10]: 'hello' In [13]: s[6:] Out[13]: 'world' In [18]: s[1:2] Out[18]: 'e' In [20]: s[:5] Out[20]: 'hello' In [24]: s[-1:] Out[24]: 'd' In [25]: s[-5:] Out[25]: 'world' In [26]: s[-5:-1] Out[26]: 'worl' In [27]: s[-5:0] Out[27]: '' In [28]: s[-5:-2] Out[28]: 'wor' In [29]: s[:] Out[29]: 'hello world' In [30]: s[::1] Out[30]: 'hello world' In [31]: s[::2] Out[31]: 'hlowrd' In [32]: s[2:8:2] Out[32]: 'low'
格式化字符串
%s
%d
string="str %s,%d"
string_templae % "替换的数据"
In [34]: note="I have %d subjects,and i love %s" In [35]: note Out[35]: 'I have %d subjects,and i love %s' In [36]: note %(5,'english') Out[36]: 'I have 5 subjects,and i love english' In [37]: d1=(5,'english') In [38]: d2=(8,"math") In [39]: note %d1 Out[39]: 'I have 5 subjects,and i love english' In [40]: note %d2 Out[40]: 'I have 8 subjects,and i love math' In [41]: type(d1) Out[41]: tuple
字符串的遍历
In [35]: s="abcdefg" In [36]: s[0] Out[36]: 'a' In [37]: for i in s: ...: print(i) ...: a b c d e f g
字符串对象方法
http://www.cnblogs.com/HKUI/articles/8970041.html
列表list操作
list.append()
list.insert()
list.extend()
list.pop()
list.remove()
list,sort()
list.sorted()
sum([1,2,3]) 对整型列表求和
In [28]: a=[1,2,3,'aa','bb'] In [29]: a[:2] Out[29]: [1, 2] In [30]: a+['c',"d"] Out[30]: [1, 2, 3, 'aa', 'bb', 'c', 'd'] In [31]: a*2 Out[31]: [1, 2, 3, 'aa', 'bb', 1, 2, 3, 'aa', 'bb'] In [32]: a-[1] --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-32-e428de774a53> in <module>() ----> 1 a-[1] TypeError: unsupported operand type(s) for -: 'list' and 'list' In [33]: a+[1] Out[33]: [1, 2, 3, 'aa', 'bb', 1]
列表元素可变,字符串可元组不可变
In [38]: s Out[38]: 'abcdefg' In [39]: s[0]='k' --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-39-ab359d50dcca> in <module>() ----> 1 s[0]='k' TypeError: 'str' object does not support item assignment In [40]: s1=['a','b','c','d'] In [41]: s1 Out[41]: ['a', 'b', 'c', 'd'] In [42]: s1[0]='k' In [43]: s1 Out[43]: ['k', 'b', 'c', 'd']
In [44]: s1 Out[44]: ['k', 'b', 'c', 'd'] In [46]: s1.append("e") In [47]: s1 Out[47]: ['k', 'b', 'c', 'd', 'e'] In [48]: s1.insert(1,'o') In [49]: s1 Out[49]: ['k', 'o', 'b', 'c', 'd', 'e'] In [50]: a=["a",'b','c'] In [51]: b=["d",'e','f'] In [52]: a.extend(b) In [53]: a Out[53]: ['a', 'b', 'c', 'd', 'e', 'f'] In [56]: a Out[56]: ['a', 'b', 'c'] In [57]: b=["c",'d','e'] In [58]: a.append(b) In [59]: a Out[59]: ['a', 'b', 'c', ['c', 'd', 'e']] In [60]: a[1] Out[60]: 'b' In [61]: a[3] Out[61]: ['c', 'd', 'e'] In [62]: a[3][1] Out[62]: 'd' In [65]: a.remove('c') In [66]: a Out[66]: ['a', 'b', ['c', 'd', 'e']] In [67]: a.pop(-2) Out[67]: 'b' In [68]: a Out[68]: ['a', ['c', 'd', 'e']] In [70]: n Out[70]: [2, 1, 3, 5, 7] In [72]: n.sort() In [73]: n Out[73]: [1, 2, 3, 5, 7] In [87]: n=[6,7,2,1] In [88]: n.sort(reverse=True) In [89]: n Out[89]: [7, 6, 2, 1] In [90]: n=[6,7,2,1] In [91]: sorted(n) Out[91]: [1, 2, 6, 7] In [92]: n Out[92]: [6, 7, 2, 1]
列表元素可变和内存
In [109]: box Out[109]: ['a', 'b', 'c'] In [110]: id(box) Out[110]: 114256351368 In [111]: x='x' In [112]: id(x) Out[112]: 114215701840 In [113]: id(box[0]) Out[113]: 114216537928 In [114]: box[0]='x' In [115]: id(box) Out[115]: 114256351368 In [116]: id(box[0]) Out[116]: 114215701840
列表的赋值和复制
值传递和引用传递
list(list)
In [21]: box=['a','b','c'] In [22]: id(box) Out[22]: 231716803400 In [23]: b1=box In [24]: id(b1) Out[24]: 231716803400 In [25]: b2=list(box) In [26]: id(b2) Out[26]: 231715699464 In [27]: b1[0]="x" In [28]: box Out[28]: ['x', 'b', 'c'] In [29]: b2[1]="bb" In [30]: box Out[30]: ['x', 'b', 'c'] In [31]: b2 Out[31]: ['a', 'bb', 'c']
元组
里面元素一旦定义不可变
拆分 x,y=(v1,v2) 必须个数对应
操作:同字符串,列表
In [94]: t=("x",'y',['a','b']) In [95]: type(t) Out[95]: tuple In [96]: x,y,z=t In [97]: x Out[97]: 'x' In [98]: y Out[98]: 'y' In [99]: z Out[99]: ['a', 'b'] In [101]: t[0] Out[101]: 'x' In [102]: t[0]="xx" --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-102-4199c23e8e8e> in <module>() ----> 1 t[0]="xx" TypeError: 'tuple' object does not support item assignment In [104]: a=1 In [105]: b=2 In [106]: t1=(a,b) In [107]: t1 Out[107]: (1, 2)

浙公网安备 33010602011771号