不得已,要学习python3了,之前了解到py2与py3有很大不同,不过学起来才能感觉到,比如print。
不过,同样的代码,可以使用py3,py2执行,结果也相似,大家可以看看。
大概因为初学,还未找到巨大差异处,比如有些函数、方法在py3中已经被弃用了
代码如下:
1 #!urs/bin/python3 2 #coding:utf-8 3 4 #定义变量a,b,c并赋值 5 a,b,c=1,5.3,"sub2020" 6 #输出变量赋值类型 7 print (type(a),type(b),type(c)) 8 9 #输出字符串c的长度 10 print ("len(c):" ,len(c)) 11 #输出c 12 print (c) 13 #输出c的 第一个[0] 到: 倒数第二个[-1] 之间的字符 14 print ("(c[0:-1]):" ,(c[0:-1])) 15 #输出第一个字符 16 print ("(c[0]):" ,(c[0])) 17 #输出c 索引[1]-[6]之间的字符 18 print ("(c[1:6]):", (c[1:6])) 19 20 #Python中的字符串有两种索引方式,从左往右以0开始,从右往左以-1开始 21 #输出 索引[-1]-[-4]之间的字符 22 print ("(c[-1:-4]):", (c[-1:-4])) 23 #输出 索引[-4]-[-1]之间的字符 24 print ("(c[-4:-1]):", (c[-4:-1])) 25 #输出索引[2]以后的字符 26 print ("(c[2:]):", (c[2:])) 27 #输出2次c 28 print ("(c*2):", (c*2)) 29 #输出两个字符串链接后的结果 30 print ('(c+"WWW"):', (c+"WWW")) 31 32 print ("*"*60) 33 list1=['sub',2020,'sub2020',20.20,'http'] 34 list2=['www',[1,2,3]] 35 36 print ("list1 :" ,list1) 37 print ("list2 :" ,list2) 38 print ("len(list1) :" , len(list1)) 39 print ("len(list2) :", len(list2)) 40 print ("(list1[0:4]) :", (list1[0:4])) 41 # print ("(list1[-1]) :" (list1[-1])) 输出错误,list无法用负值索引 42 print ("list2*2 :", list2*2) 43 print ("list1+list2 :", list1+list2) 44 45 #List中的元素是可以改变的 46 list1[4]="new" 47 print ("new list1 :" ,list1) 48 49 print ("*"*60) 50 tuple1=('sub',2020,'sub2020',20.20,'http') 51 tuple2=('www',[1,2,3]) 52 53 print ("tuple1 :" ,tuple1) 54 print ("tuple2 :" ,tuple2) 55 print ("len(tuple1) :" , len(tuple1)) 56 print ("len(tuple2) :", len(tuple2)) 57 print ("(tuple1[0:4]) :", (tuple1[0:4])) 58 #元组有两种索引方式,从左往右以0开始,从右往左以-1开始 59 print ("tuple1[-1] :" ,(tuple1[-1])) 60 print ("tuple2*2 :", tuple2*2) 61 print ("tuple1+tuple2 :", tuple1+tuple2) 62 63 #tuple中的元素不可改变 64 #tuple1[4]="new" 65 #print ("new tuple1 :" ,tuple1)
py3 output:
<class 'int'> <class 'float'> <class 'str'> len(c): 7 sub2020 (c[0:-1]): sub202 (c[0]): s (c[1:6]): ub202 (c[-1:-4]): (c[-4:-1]): 202 (c[2:]): b2020 (c*2): sub2020sub2020 (c+"WWW"): sub2020WWW ************************************************************ list1 : ['sub', 2020, 'sub2020', 20.2, 'http'] list2 : ['www', [1, 2, 3]] len(list1) : 5 len(list2) : 2 (list1[0:4]) : ['sub', 2020, 'sub2020', 20.2] list2*2 : ['www', [1, 2, 3], 'www', [1, 2, 3]] list1+list2 : ['sub', 2020, 'sub2020', 20.2, 'http', 'www', [1, 2, 3]] new list1 : ['sub', 2020, 'sub2020', 20.2, 'new'] ************************************************************ tuple1 : ('sub', 2020, 'sub2020', 20.2, 'http') tuple2 : ('www', [1, 2, 3]) len(tuple1) : 5 len(tuple2) : 2 (tuple1[0:4]) : ('sub', 2020, 'sub2020', 20.2) tuple1[-1] : http tuple2*2 : ('www', [1, 2, 3], 'www', [1, 2, 3]) tuple1+tuple2 : ('sub', 2020, 'sub2020', 20.2, 'http', 'www', [1, 2, 3])
py2 output
(<type 'int'>, <type 'float'>, <type 'str'>) ('len(c):', 7) sub2020 ('(c[0:-1]):', 'sub202') ('(c[0]):', 's') ('(c[1:6]):', 'ub202') ('(c[-1:-4]):', '') ('(c[-4:-1]):', '202') ('(c[2:]):', 'b2020') ('(c*2):', 'sub2020sub2020') ('(c+"WWW"):', 'sub2020WWW') ************************************************************ ('list1 :', ['sub', 2020, 'sub2020', 20.2, 'http']) ('list2 :', ['www', [1, 2, 3]]) ('len(list1) :', 5) ('len(list2) :', 2) ('(list1[0:4]) :', ['sub', 2020, 'sub2020', 20.2]) ('list2*2 :', ['www', [1, 2, 3], 'www', [1, 2, 3]]) ('list1+list2 :', ['sub', 2020, 'sub2020', 20.2, 'http', 'www', [1, 2, 3]]) ('new list1 :', ['sub', 2020, 'sub2020', 20.2, 'new']) ************************************************************ ('tuple1 :', ('sub', 2020, 'sub2020', 20.2, 'http')) ('tuple2 :', ('www', [1, 2, 3])) ('len(tuple1) :', 5) ('len(tuple2) :', 2) ('(tuple1[0:4]) :', ('sub', 2020, 'sub2020', 20.2)) ('tuple1[-1] :', 'http') ('tuple2*2 :', ('www', [1, 2, 3], 'www', [1, 2, 3])) ('tuple1+tuple2 :', ('sub', 2020, 'sub2020', 20.2, 'http', 'www', [1, 2, 3])) Traceback (most recent call last): File "basic_data_type.py", line 66, in <module> tuple2[1]=[1,2,3,4] TypeError: 'tuple' object does not support item assignment ***Repl Closed***
quote:http://www.runoob.com/python3/python3-data-type.html