Python数据类型
1。数值 --类型: 1/整型
2/长整型
3/浮点型 -- 0.0, 12.0, -18.8, 3e+7等
4/复数型 -- complex
In [1]: 0x34al Out[1]: 842L --0x34a --表示16进制 --l --表示长整型
In [2]: 4e+7 Out[2]: 40000000.0 In [3]: type(4e+7) Out[3]: float --科学计数法
In [4]: b = "hello lulu" In [6]: b Out[6]: 'hello lulu' In [7]: print b hello lulu --里面有换行符,会把它打印出来 In [8]: a = '''hello ...: lulu''' In [9]: a Out[9]: 'hello lulu' In [10]: print a hello lulu ---三重引号 -可以是双引号 也可以是单引号 --表解释 这样可以自动把换行符加载 ,不需要自己写
2. 字符串 --是一个序列 -- 对于序列的操作 可以通过索引和切片
In [7]: b = 'oplkdj' In [8]: b Out[8]: 'oplkdj' In [10]: b[0] Out[10]: 'o' In [11]: b[4] Out[11]: 'd' In [12]: b[-1] Out[12]: 'j' In [13]: b[-3] Out[13]: 'k' ========================== /* 取两个字符 */ In [14]: b[0]+b[4] Out[14]: 'od' In [15]: b[0:3] --从第0个开始取到3个,第三个不显示 Out[15]: 'opl'
In [2]: a = 'abcde' In [3]: a Out[3]: 'abcde' In [4]: a[1:2] Out[4]: 'b' In [5]: a[:] Out[5]: 'abcde' In [6]: a[:-1] Out[6]: 'abcd' --0到最后一个,但不取最后一个 In [7]: a[:2] Out[7]: 'ab' --[:2]默认冒号左边为0 In [8]: a[-1] Out[8]: 'e' In [9]: a[1:] Out[9]: 'bcde' --从1号位取到最后
In [10]: a[:-1] Out[10]: 'abcd' In [11]: a[::-1] Out[11]: 'edcba' In [12]: a[::1] Out[12]: 'abcde' In [13]: a[::2] Out[13]: 'ace' In [14]: a[::] Out[14]: 'abcde'
In [3]: a[-4:-2] Out[3]: 'bc' In [4]: a[-2:-4:-1] Out[4]: 'dc'
/* 序列*/ In [5]: a Out[5]: 'abcde' In [6]: len(a) Out[6]: 5 In [8]: a + 'f' Out[8]: 'abcdef' --连接两个序列 //重复序列元素 In [9]: a * 5 Out[9]: 'abcdeabcdeabcdeabcdeabcde' In [10]: '2' * 5 Out[10]: '22222' //a为字符串,1为整数,数据类型不符合 In [11]: a + 1 --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-11-a1bd27f4633f> in <module>() ----> 1 a + 1 TypeError: cannot concatenate 'str' and 'int' objects ==================================== //判断某元素是否存在某个变量中 In [12]: 'a' in a Out[12]: True In [13]: a Out[13]: 'abcde' In [14]: 'f' in a Out[14]: False //不存在 In [15]: 'f' not in a Out[15]: True In [16]: 'f' not in a+'f' Out[16]: False ==================================== //判断最大最小值 In [17]: a Out[17]: 'abcde' In [18]: max(a) Out[18]: 'e' In [19]: min(a) Out[19]: 'a' ==================================== //比较两个序列是否相等,小于则显示“-1”,大于则显示“1” In [20]: cmp(a,'abcde') Out[20]: 0 In [21]: cmp(a,'abcdef') Out[21]: -1 In [22]: cmp(a,'ab') Out[22]: 1 In [23]: cmp(a,'abcd') Out[23]: 1
3. 列表
//基本模式 In [46]: list1 =[] In [47]: type(list1) Out[47]: list In [48]: list1 Out[48]: [] In [49]: list2 = ['a',1,(1,),['hello','python']] In [50]: list2 Out[50]: ['a', 1, (1,), ['hello', 'python']] In [51]: type(list2) Out[51]: list //元组数量 In [52]: len(list2) Out[52]: 4 //list的第二个元素 In [53]: list2[2] Out[53]: (1,) //替换列表的元素 In [54]: list2[0] = 'b' In [55]: list2[0] Out[55]: 'b' In [56]: list3 = ['linux'] In [57]: list2 + list3 Out[57]: ['b', 1, (1,), ['hello', 'python'], 'linux'] //往list中新增元素 In [58]: list3.append('fra') In [59]: list3 Out[59]: ['linux', 'fra']
In [60]: list1 Out[60]: [] //删除某个list In [61]: del list1 In [62]: list1 --------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-62-8304c57391a2> in <module>() ----> 1 list1 NameError: name 'list1' is not defined In [63]: list2 Out[63]: ['b', 1, (1,), ['hello', 'python']] //删除某个元素 In [64]: del list2[-1] ================================== In [65]: list2 Out[65]: ['b', 1, (1,)] In [67]: list2 Out[67]: ['b', 1, (1,), 1] //如果有两个重复的元素,就删除第一个 In [68]: list2.remove(1) In [69]: list2 Out[69]: ['b', (1,), 1]
4. 元组
In [24]: t = ('a',1,(1,)) In [25]: t Out[25]: ('a', 1, (1,)) In [26]: type(t) Out[26]: tuple //元组 In [30]: t1 = (1) In [31]: type(t1) Out[31]: int In [32]: t1 = (1,) In [33]: type(t1) Out[33]: tuple //加上 逗号,则表示为元组
In [34]: t = (a, 'b', 'c') In [35]: t Out[35]: ('abcde', 'b', 'c') // a为变量, 显示时 将变量数值替换 In [36]: first, second, third = t In [37]: first Out[37]: 'abcde' In [38]: second Out[38]: 'b' In [39]: third Out[39]: 'c'
In [40]: t.count('b') Out[40]: 1 In [41]: t.count('bc') Out[41]: 0 In [42]: t.count('abc') Out[42]: 0 In [43]: t Out[43]: ('abcde', 'b', 'c') --- 如果在元组里存在此元素 ,显示“1” ,不存在显示“0"
//插入列表 In [87]: list1 Out[87]: [] In [88]: list3 Out[88]: ['linux', 'fra'] //(元素的位置, 添加的元组) In [89]: list3.insert(2, list1) In [90]: list3 Out[90]: ['linux', 'fra', []] In [91]: list2 Out[91]: ['b', (1,), 1] In [92]: list2.insert(1, list1) In [93]: list2 Out[93]: ['b', [], (1,), 1]
/* 给列表排序 */ In [94]: list3.sort() In [95]: list3 Out[95]: [[], 'fra', 'linux']
/* 给列表反转,最后一个变成第一个 */ In [96]: list2.reverse() In [97]: list2 Out[97]: [1, (1,), [], 'b'] In [98]: list2.reverse() In [99]: list2 Out[99]: ['b', [], (1,), 1]
In [3]: list1 = [(1,),'fra','linux'] In [4]: list1 Out[4]: [(1,), 'fra', 'linux'] In [5]: range(5) Out[5]: [0, 1, 2, 3, 4] In [6]: list1.extend(range(5)) In [7]: list1 Out[7]: [(1,), 'fra', 'linux', 0, 1, 2, 3, 4] In [8]: list1.extend('abc') In [9]: list1 Out[9]: [(1,), 'fra', 'linux', 0, 1, 2, 3, 4, 'a', 'b', 'c'] In [11]: list1.extend(('t1','t2')) In [12]: list1 Out[12]: [(1,), 'fra', 'linux', 0, 1, 2, 3, 4, 'a', 'b', 'c', 't1', 't2']
5. 字典
In [13]: info = 'Tom 180 Male' In [14]: info[1:3] Out[14]: 'om' In [15]: info[0:3] Out[15]: 'Tom' /* 在列表中,取到3,但不会显示3。 在字典中,取到几,就会显示几号位。 */
In [20]: list1 = ['name', 'age'] In [21]: list2 = ['tom', '20'] In [22]: list3 = ['mike',30] In [23]: zip(list1, list2) Out[23]: [('name', 'tom'), ('age', '20')] In [24]: zip(list1, list3) Out[24]: [('name', 'mike'), ('age', 30)]
In [25]: dic = {} In [26]: type(dic) Out[26]: dict In [27]: dic = {'a':1, 1:123} In [28]: dic Out[28]: {1: 123, 'a': 1} In [29]: dic = {'a':1, 1:123, ('a','b'):'hello'} In [30]: len(dic) Out[30]: 3 //获取所有keys In [31]: dic.keys() Out[31]: ['a', 1, ('a', 'b')] //获取所有values In [32]: dic.values() Out[32]: [1, 123, 'hello'] //获取keys的值 In [33]: dic.get(1) Out[33]: 123 In [36]: dic Out[36]: {1: 123, 'a': 1, ('a', 'b'): 'hello'} //修改某个keys的values In [37]: dic['a'] = 3 In [38]: dic Out[38]: {1: 123, 'a': 3, ('a', 'b'): 'hello'} In [39]: dic[('a', 'b')] = 'linux' In [40]: dic Out[40]: {1: 123, 'a': 3, ('a', 'b'): 'linux'} //在外部定义一个keys和它的values In [41]: dic.get('b', 'python') Out[41]: 'python' //是否有keys In [43]: dic.has_key('a') Out[43]: True In [44]: dic.has_key('b') Out[44]: False //将每一个keys与values都以元组形式展现 In [45]: dic.items() Out[45]: [('a', 3), (1, 123), (('a', 'b'), 'linux')] In [46]: dic Out[46]: {1: 123, 'a': 3, ('a', 'b'): 'linux'} //拷贝 In [47]: dic1 = dic.copy() In [48]: dic1 Out[48]: {1: 123, 'a': 3, ('a', 'b'): 'linux'} In [49]: dic Out[49]: {1: 123, 'a': 3, ('a', 'b'): 'linux'}
//更新,则最前面加入新的 In [50]: dic Out[50]: {1: 123, 'a': 3, ('a', 'b'): 'linux'} In [51]: dic1 = {3:3, 4:4} In [52]: dic.update(dic1) In [53]: dic Out[53]: {1: 123, 3: 3, 4: 4, 'a': 3, ('a', 'b'): 'linux'} //打包后,将其以字典形式呈现 In [54]: zip(list1, list2) Out[54]: [('name', 'tom'), ('age', '20')] In [55]: dict(zip(list1, list2)) Out[55]: {'age': '20', 'name': 'tom'}
/* 以下两种,作用相同,给某个keys赋值*/ In [57]: dict([('a', 10), ('b', 20)]) Out[57]: {'a': 10, 'b': 20} In [58]: dic.fromkeys('ab',100) Out[58]: {'a': 100, 'b': 100}