一、数据类型:
- 数值- 字符串- 列表- 元组- 字典
1.数值类型:
(1)整型
In [6]: a = 123 In [7]: type(a) Out[7]: int In [8]:
(2)长整型
In [8]: a = 199999999999999999999999999999 In [9]: a Out[10]: 199999999999999999999999999999L In [11]: type(a) Out[12]: long In [13]:
(3)浮点型
0.0, 12.0 -18.8 3e+7等 科学计数法是浮点型 In [11]: 3e+7 Out[11]: 30000000.0 In [12]: type(3e+7) Out[12]: float In [13]: 3.0/2 Out[13]: 1.5 In [14]: type(3.0/2) Out[14]: float In [15]:
(4)复数型
python对复数提供内嵌支持,这是大部分软件没有的。 In [8]: a = 3.14j In [9]: a Out[9]: 3.14j In [10]: type(a) Out[10]: complex
2.字符串类型:
In [12]: a = 'abc' In [13]: a Out[13]: 'abc' In [14]: type(a) Out[14]: str In [15]: 三重引号还可以做注释:. In [28]: a = 'hello\nworld' In [29]: a Out[29]: 'hello\nworld' In [30]: a = "hello\nworld" In [31]: a Out[31]: 'hello\nworld' In [39]: a = '''hello\nworld''' In [40]: a Out[40]: 'hello\nworld' In [41]: print a hello world In [42]: In [43]: type(a) Out[44]: str
序列索引:
In [42]: a = 'abcde' In [43]: a[0] Out[43]: 'a' In [44]: a[1] Out[44]: 'b' In [45]: a[-1] Out[45]: 'e' In [46]: a[-2] Out[46]: 'd'
序列切片:
In [42]: a = 'abcde' In [43]: a[0] Out[43]: 'a' In [44]: a[1] Out[44]: 'b' In [45]: a[-1] Out[45]: 'e' In [46]: a[-2] Out[46]: 'd' In [47]: a[0:2] Out[47]: 'ab' In [48]: a[0:4] Out[48]: 'abcd' In [49]: a[0:3] Out[49]: 'abc' In [50]: a[1:3] Out[50]: 'bc' In [56]: a[0] + a[1] Out[56]: 'ab' In [57]: a[:2] Out[57]: 'ab' In [58]: a[:] Out[58]: 'abcde' In [59]: a[:-1] Out[59]: 'abcd' In [60]: a[::-1] Out[60]: 'edcba' In [61]: a[::1] Out[61]: 'abcde' In [62]: a[:3:1] Out[62]: 'abc' In [63]: a[::2] Out[63]: 'ace' In [64]: a Out[64]: 'abcde' In [65]: a[-4::-2] Out[65]: 'b' In [66]: a[-4:-2] Out[66]: 'bc' In [67]: a[-2:-4:-1] Out[67]: 'dc' In [68]:
二、元组
1.序列
字符串、列表和元组都是序列 序列的两个主要特点是索引操作符和切片操作符.
索引操作符让我们可以从序列中抓取一个特定的项目。 切片操作符让我们能够获取序列的一个切片,即一部分序列。 序列的基本操作:
(1) len() :求序列长度
In [1]: a = '1234;5lasdfasdf' In [2]: len(a) Out[2]: 15
(2) +:连接2个序列(两个str)
In [3]: a = 'hello' In [4]: b = 'world' In [5]: print a+b helloworld In [6]:
(3) *:重复序列元素(*后面根的是int)
In [9]: print a * 3 hellohellohello In [10]:
(4) in:判断元素是否在序列中
In [12]: print a [1, 3, 3, 5] In [13]: 3 in a Out[13]: True
(5) max():返回最大值
In [21]: print a [1, 3, 3, 5] In [22]: print max(a) 5
(6) min():返回最小值
In [21]: print a [1, 3, 3, 5] n [23]: print min(a) 1
(7) cmp(x,y):比较两个序列是否相等
In [24]: a Out[24]: [1, 3, 3, 5] In [27]: b = [1,3,3,5] In [28]: cmp (a,b) Out[28]: 0 In [29]: cmp(a,'1,3,3,5') Out[29]: -1 In [30]: cmp(a,[1,3,3,5]) Out[30]: 0 In [31]: cmp(a[1],a[2]) Out[31]: 0 In [32]: cmp(a[0],a[3]) Out[32]: -1 In [33]: cmp(a[3],a[2]) Out[33]: 1 In [34]:
元组:
- 元组和列表十分相似- 元组和字符串一样是不可以变的。
元组可以存储一系列的值 元组通常用在用户定义的函数能够安全地采用一组值的时候,即被使用的元组的值不会改变。 In [34]: t = (1,3,5,'a',(1,))
In [35]: type(t) Out[35]: tuple In [36]: print t (1, 3, 5, 'a', (1,)) In [37]:
2.元组操作:
元组和字符串一样属于序列类型,可以通过索引和切片操作 元组值不可变 无组的拆分 t = (1,2,3) a,b,c=t In [150]: t = (1, 3, 5, 'a', (1,)) In [151]: print t (1, 3, 5, 'a', (1,)) In [39]: t[0] Out[39]: 1 In [40]: print t (1, 3, 5, 'a', (1,)) In [41]: t[0] = 2---------------------------------------------------------------------------TypeError Traceback (most recent call last)in ()----> 1 t[0] = 2TypeError: 'tuple' object does not support item assignmentIn [42]:
(1) 元组中引用变量:
In [42]: a Out[42]: [1, 3, 3, 5] In [43]: t = (a,'ab') In [44]: t Out[44]: ([1, 3, 3, 5], 'ab') In [45]:
(2) 元组切片:
In [44]: tOut[44]: ([1, 3, 3, 5], 'ab')In [45]: first,second = t In [46]: firstOut[46]: [1, 3, 3, 5]In [47]: secondOut[47]: 'ab'In [14]: tOut[14]: (1, 2, 3)In [15]: t.index(1)Out[15]: 0In [16]: t.index(2)Out[16]: 1In [17]: t.index(3)Out[17]: 2In [18]: t = (1,3,3,2,5) In [19]: t.index(5)Out[19]: 4In [20]: t.count(3)Out[20]: 2In [21]: t.count(5)Out[21]: 1
三、列表
列表内可变 列表(list)是处理一组有序项目的数据结构,即要以在列表中存储一个序列的项目。 列表显示是可变类型的数据
1.创建列表:
list1 = [] list2 = [1,3,3,'a'] list3 = list() In [22]: list1 = [] In [23]: type(list1) Out[23]: listIn [24]: listlist list1 In [24]: list2 = list() In [25]: type(list2) Out[25]: listIn [26]: listlist list1 list2 In [26]: list2 Out[26]: [] In [27]: listlist list1 list2 In [27]: list3 = ['a',1,2,(1,),[1,'a']] In [28]: list3 Out[28]: ['a', 1, 2, (1,), [1, 'a']] In [29]: len(list3) Out[29]: 5In [30]: In [30]: list3[-1] Out[30]: [1, 'a'] In [31]: list3[0] Out[31]: 'a'In [37]: list3 Out[37]: ['a', 1, 2, (1,), [1, 'a']] In [38]: list3[0:3] Out[38]: ['a', 1, 2] In [40]: list3[0] = 'b'In [41]: list3 Out[41]: ['b', 1, 2, (1,), [1, 'a']]
2.列表操作:
(1) 取值:
切片和索引
(2) 添加:(append()默认加到最后一个元素)
list.append() In [85]: list3 Out[85]: ['b', [], (1,), 1] In [86]: list3[1] Out[86]: [] In [87]: list3[1].append('abc') In [88]: list3 Out[88]: ['b', ['abc'], (1,), 1]
(3) 追加
In [42]: list3.append(333) In [43]: list3 Out[43]: ['b', 1, 2, (1,), [1, 'a'], 333] In [44]: In [43]: list3 Out[43]: ['b', 1, 2, (1,), [1, 'a'], 333] In [44]: list2 Out[44]: [] In [45]: list2 = [1111,222,333] In [46]: list2 + list3 Out[46]: [1111, 222, 333, 'b', 1, 2, (1,), [1, 'a'], 333] In [47]: In [52]: (list2 + list3)*2Out[52]: [1111,222,333,'b',1,2, (1,), [1, 'a'],333,1111,222,333,'b',1,2, (1,), [1, 'a'],333] In [53]:
(4) 删除
del list[]list.remove(list[])In [53]: list2Out[53]: [1111, 222, 333]In [54]: del listlist list1 list2 list3In [54]: del list2[2]In [55]: list2Out[55]: [1111, 222]In [55]: list2Out[55]: [1111, 222]In [56]: list2.remove(list2[1])In [57]: list2Out[57]: [1111]
(5) 修改:
list[] = xIn [74]: list2 Out[74]: [1111, 22222, 2223, 22233434, 33434]In [75]: list2[0] = 0000 In [76]: list2 Out[76]: [0, 22222, 2223, 22233434, 33434]In [77]: list2 Out[77]: [0, 22222, 2223, 22233434, 33434]In [78]: list2.insert(1,66666) In [79]: list2 Out[79]: [0, 66666, 22222, 2223, 22233434, 33434]In [80]:
(6) 查找:
var in listIn [57]: list2Out[57]: [1111]In [58]: 1 in list2Out[58]: FalseIn [59]: 1111 in list2Out[59]: TrueIn [60]:
(7) 排序:
In [90]: list3 Out[90]: ['b', ['abc'], (1,), 1] In [91]: list3.sort() In [92]: list3 Out[92]: [1, ['abc'], 'b', (1,)] In [93]:
(8) 反转:
In [93]: list3 Out[93]: [1, ['abc'], 'b', (1,)] In [94]: list3.reverse() In [95]: list3 Out[95]: [(1,), 'b', ['abc'], 1] In [96]:
(9) POP默认删除最后一个,pop删除对应位置.
In [103]: list2Out[103]: [0, 66666, 22222, 2223, 22233434, 33434]In [104]: list2.pop(1)Out[104]: 66666In [105]: list2Out[105]: [0, 22222, 2223, 22233434, 33434]In [106]: list2.pop()Out[106]: 33434In [107]: list2.pop()Out[107]: 22233434In [108]: list2Out[108]: [0, 22222, 2223]In [109]:
(10) 迭代:
字符串,元组都是可迭代的,都可以通过for循环去访问。
In [120]: list3 Out[120]: ['b', ['abc']] In [121]: range(5) Out[121]: [0, 1, 2, 3, 4] In [122]: list3.extend(range(5)) In [123]: list3 Out[123]: ['b', ['abc'], 0, 1, 2, 3, 4] In [125]: list3.extend('5555') In [126]: list3 Out[126]: ['b', ['abc'], 0, 1, 2, 3, 4, '5', '5', '5', '5'] In [127]: list3.extend('a') In [128]: list3 Out[128]: ['b', ['abc'], 0, 1, 2, 3, 4, '5', '5', '5', '5', 'a'] In [129]: list3.extend('1234') In [130]: list3 Out[130]: ['b', ['abc'], 0, 1, 2, 3, 4, '5', '5', '5', '5', 'a', '1', '2', '3', '4'] In [131]:
-
tab补齐:
-
python 一切都是对象
(11) 帮助:
help(t.index)
Help on built-in function index:index(...) L.index(value, [start, [stop]]) -> integer -- return first index of value. Raises ValueError if the value is not present. (END)