- 基本数据类型
1、数字
int(整型)
在32位机器上,整数的位数为32位,取值范围为-2**31~2**31-1,即-2147483648~2147483647
在64位系统上,整数的位数为64位,取值范围为-2**63~2**63-1,即-9223372036854775808~9223372036854775807
在64位系统上,整数的位数为64位,取值范围为-2**63~2**63-1,即-9223372036854775808~9223372036854775807
1 1)int --> string 2 str是保留关键字, 3 >>>a = 10 4 >>>str1 = str(a) 5 6 2)string --> int 7 >>>x = "10" 8 >>>y = int(x) 1 >>> lists = ['a', 'b', 'e', 'c', 'd', 'a'] 2 >>> print '列表list转换为str:', ''.join(lists) 3 结果: 4 列表list转换为str: abecda 5 6 >>>strs = 'hongten' 7 >>>print '序列strs转换为list:', list(strs) 8 结果: 9 序列strs转换为list:['h', 'o', 'n', 'g', 't', 'e', 'n'] 10 11 >>>print '列表list转换为tuple:', tuple(lists) 12 结果: 13 列表list转换为tuple:('h', 'o', 'n', 'g', 't', 'e', 'n') 14 15 >>>print '整数转换为字符chr:', chr(67) 16 结果: 17 整数转换为字符chr:'C' 18 19 >>>print('字符chr转换为整数:', ord('C')) 20 结果: 21 字符chr转换为整数:67 22 23 >>>print('整数转16进制数:', hex(12)) 24 结果:: 25 整数转16进制数:'0xc' 26 27 >>>print('整数转8进制数:', oct(12)) 28 结果: 29 整数转8进制数:'014'
2、布尔值
在计算机内部,python使用1来表示True,而使用0来表示False。可以使用int数将布尔值转换为一个整数。
1 实例: 2 >>>print(int(True)) 3 >>>1 4 5 >>>print(int(False)) 6 >>>0
也可以用bool函数将一个数字值转换成一个布尔值。如果值为0,这个函数返回False; 否则,这个函数总是返回True。
1 实例: 2 >>>print(bool(0)) 3 >>>False 4 5 >>>print(bool(1)) 6 >>>True
3、字符串
a、处理字符串的函数
1 >>> s = "Welcome" 2 >>> len(s) 3 7 4 >>> max(s) 5 'o' 6 >>> min(s) 7 'W'
b、下标运算符
1 >>> s = "Welcome" 2 >>>for i in range(0, len(s), 2): 3 print(s[i], end = '') 4 >>>Wloe 5 6 >>>s = "Welcome" 7 >>>s[-1] 8 'e' 9 >>>s[-2] 10 'm'
c、截取运算符
1 >>> s = "Welcome" 2 >>> s[1:4] 3 'elc' 4 >>> s[ :6] 5 'Welcom' 6 >>> s[4: ] 7 'ome' 8 >>> s[1:-1] 9 'elcom'
d、in和not in 运算符
1 >>> s = "Welcome" 2 >>> 'come' in s 3 True 4 >>> 'come' not in s 5 False
e、比较字符串
1 >>> "green" == "glow" 2 False 3 >>> "green" != "glow" 4 True 5 >>> "green" > "glow" 6 True 7 >>> "green" < "glow" 8 False 9 >>> "green" <= "glow" 10 False
f、搜索字串
1 >>> s = "welcome to python" 2 >>> s.endswith('thon') 3 True 4 >>> s.startswith("good") 5 False 6 >>> s.find("come") 7 3 8 >>> s.find("become") 9 -1 10 >>> s.find("o") 11 4 12 >>> s.rfind("o") 13 15 14 >>> s.count("o") 15 3
g、转换字符串
1 >>> s = "welcome to python" 2 >>> s1 = s.capitalize() 3 >>> s1 4 'Welcome to python' 5 >>> s2 = s.title() 6 >>> s2 7 'Welcome To Python' 8 >>> s = "New England" 9 >>> s3 = s.lower() 10 >>> s3 11 'new england' 12 >>> s4 = s.upper() 13 >>> s4 14 'NEW ENGLAND' 15 >>> s5 = s.swapcase() 16 >>> s5 17 'nEW eNGLAND' 18 >>> s6 = s.replace("England", "Haven") 19 >>> s6 20 'New Haven' 21 >>> s 22 'New England'
H、删除字符串的空格
1 >>> s = " Welcome to Python " 2 >>> s1 = s.lstrip() 3 >>> s1 4 'Welcome to Python ' 5 >>> s2 = s.rstrip() 6 >>> s2 7 ' Welcome to Python' 8 >>> s3 = s.strip() 9 >>> s3 10 'Welcome to Python'
I、格式化字符串
1 >>> s = "Welcome" 2 >>> s1 = s.center(11) 3 >>> s1 4 ' Welcome ' 5 >>> s2 = s.ljust(11) 6 >>> s2 7 'Welcome ' 8 >>> s3 = s.rjust(11) 9 >>> s3 10 ' Welcome'
4、列表
a、列表使用的函数
1 >>> list = [2,3,4,1,32] 2 >>> len(list) 3 5 4 >>> max(list) 5 32 6 >>> min(list) 7 1 8 >>> sum(list) 9 42
b、下标运算符
1 >>> list = [2,3,5,2,33,21] 2 >>> list[-1] 3 21 4 >>> list[-3] 5 2
c、列表截取
1 >>> list = [2,3,5,2,33,21] 2 >>> list[2:4] 3 [5, 2] 4 >>> list[:2] 5 [2, 3] 6 >>> list[3:] 7 [2, 33, 21] 8 >>> list[1:-3] 9 [3, 5] 10 >>> list[-4:-2] 11 [5, 2]
d、列表方法
1 >>> list = [2,3,4,1,32,4] 2 >>> list.append(19) 3 >>> list 4 [2, 3, 4, 1, 32, 4, 19] 5 >>> list.count(4) 6 2 7 >>> list2 = [99,54] 8 >>> list.extend(list2) 9 >>> list 10 [99, 25, 54, 99, 54, 99, 54] 11 >>> list.insert(1,25) 12 >>> list 13 [99, 25, 25, 54, 99, 54, 99, 54]
1 >>> list1 = [2,25,3,4,1,32,4,1,32,4,19,99,54] 2 >>> list.pop(2) 3 25 4 >>> list1 5 [2, 25, 4, 1, 32, 4, 1, 32, 4, 19, 99, 54] 6 >>> list1 = [2,25,3,4,1,32,4,1,32,4,19,99,54] 7 >>> list.pop(2) 8 25 9 >>> list 10 [99, 25, 54, 99, 54, 99, 54] 11 >>> list1 12 [2, 25, 3, 4, 1, 32, 4, 1, 32, 4, 19, 99, 54] 13 >>> 14 >>> 15 >>> list1.pop(2) 16 3 17 >>> list1 18 [2, 25, 4, 1, 32, 4, 1, 32, 4, 19, 99, 54] 19 >>> list1.pop() 20 54 21 >>> list1 22 [2, 25, 4, 1, 32, 4, 1, 32, 4, 19, 99] 23 >>> list1.remove(32) 24 >>> list1 25 [2, 25, 4, 1, 4, 1, 32, 4, 19, 99] 26 >>> list1.reverse() 27 >>> list1 28 [99, 19, 4, 32, 1, 4, 1, 4, 25, 2]29 >>> list1.sort() 30 >>> list1 31 [1, 1, 2, 4, 4, 4, 19, 25, 32, 99]
e、enumerate遍历列表
1 使用如下的方式遍历一个列表,但是缺点是无法读取索引index。 2 >>> a = [3,4,5,2,3,7,8,9] 3 >>> for i in a: 4 ... print i 5 ... 6 3 7 4 8 5 9 2 10 3 11 7 12 8 13 9 14
下面两种方法改进,如下举例: 17 >>> for index,item in enumerate(a): 18 ... print index,item 19 ... 20 0 3 21 1 4 22 2 5 23 3 2 24 4 3 25 5 7 26 6 8 27 7 9 28 >>> 29 >>> 30 >>> for i in range(len(a)): 31 ... print i,a[i] 32 ... 33 0 3 34 1 4 35 2 5 36 3 2 37 4 3 38 5 7 39 6 8 40 7 9 41 >>>
5、元组
元组跟列表类似,但是元组中的元素是固定的,也就是说,一旦一个元组被创建,就无法对元组中的元素进行添加、删除、替代或重新排序。
1 >>> tuple1 = ("green", "red", "blue") 2 >>> print tuple1 3 ('green', 'red', 'blue') 4 >>> tuple2 = tuple([7,1,2,23,4,5]) 5 >>> print tuple2 6 (7, 1, 2, 23, 4, 5)
6、字典
字典是一个存储键值对集合的容器对象。它通过使用关键字实现快速、删除和更新值。
如创建字典语句:
students = {”111-34-3434“:"John", "132-56-6290":"Peter"}
a、添加、删除举例:
1 >>> students = {"111-34-3434" : "John", "132-56-6290" : "Peter"} 2 >>> students["234-56-9010"] = "Susan" #从字典添加条目 3 >>> students["234-56-9010"] 4 'Susan' 5 >>> del students["234-56-9010"] #从字典删除一个条目
b、循环条目:
1 >>>students = {'111-34-3434': 'John Smith', '132-56-6290': 'Peter'} 2 >>> for key in students: 3 ... print(key + ":" + str(students[key])) 4 ... 5 111-34-3434:John Smith 6 132-56-6290:Peter
c、len函数:
1 >>>students = {'111-34-3434': 'John Smith', '132-56-6290': 'Peter'} 2 >>>len(students) 3 2
d、检测一个关键字是否在字典中:
1 >>>students = {'111-34-3434': 'John Smith', '132-56-6290': 'Peter'} 2 >>> "111-34-3434" in students 3 True 4 >>> "998-34-3434" in students 5 False 6 >>>
e、相等性检测:
1 >>> d1 = {"red":41, "blue":3} 2 >>> d2 = {"blue":3, "red":41} 3 >>> d1 == d2 4 True 5 >>> d1 != d2 6 False 7 >>>
f、字典方法:
1 >>> students = {"111-34-3434":"John", "132-56-6290":"Peter"} 2 >>> tuple(students.keys()) 3 ('111-34-3434', '132-56-6290') 4 >>> tuple(students.values()) 5 ('John', 'Peter') 6 >>> tuple(students.items()) 7 (('111-34-3434', 'John'), ('132-56-6290', 'Peter')) 8 >>> students.get("111-34-3434") 9 'John' 10 >>> print(students.get("999-334-3434")) 11 None 12 >>> students.pop("111-34-3434") 13 'John' 14 >>> students 15 {'132-56-6290': 'Peter'} 16 >>> students.clear() 17 >>> students 18 {} 19 >>>
1 dic = {'k1' : 123,'k2' : 456,'k4' : 111} 2 n = dic.fromkeys(['k1','k2','k3'],[]) 3 print(n) 4 n['k1'].append('x') 5 print(n) 6 7 运行结果如下: 8 {'k2': [], 'k1': [], 'k3': []} 9 {'k2': ['x'], 'k1': ['x'], 'k3': ['x']} 10 11 dic = {'k1' : 123,'k2' : 456,'k4' : 111} 12 n = dic.fromkeys(['k1','k2','k3'],'alex') 13 print(n) 14 15 运行结果如下: 16 {'k3': 'alex', 'k1': 'alex', 'k2': 'alex'} 17 18 li = [11,22,44] 19 new_dict = dict(enumerate(li)) 20 print(new_dict) 21 22 运行结果如下: 23 {0: 11, 1: 22, 2: 44} 24 25 26 li = [11,22,44] 27 new_dict = dict(enumerate(li,10)) 28 print(new_dict) 29 30 运行结果如下: 31 {10: 11, 11: 22, 12: 44} 32 33 t = (11,22,['alex',{'k1':"v1"}]) 34 t[2][1]['k2'] = 123 35 print(t) 36 37 运行结果如下: 38 (11, 22, ['alex', {'k1': 'v1', 'k2': 123}]) 39 40 t = (11,22,['alex',{'k1':"v1"}]) 41 t[2][1].update({'k2':123}) 42 print(t) 43 44 运行结果如下: 45 (11, 22, ['alex', {'k1': 'v1', 'k2': 123}])