不得已,要学习python3了,之前了解到py2与py3有很大不同,不过学起来才能感觉到,比如print。

不过,同样的代码,可以使用py3,py2执行,结果也相似,大家可以看看。

大概因为初学,还未找到巨大差异处,比如有些函数、方法在py3中已经被弃用了

代码如下:

 #!urs/bin/python3
#coding:utf-8 #定义变量a,b,c并赋值
a,b,c=1,5.3,"sub2020"
#输出变量赋值类型
print (type(a),type(b),type(c)) #输出字符串c的长度
print ("len(c):" ,len(c))
#输出c
print (c)
#输出c的 第一个[0] 到: 倒数第二个[-1] 之间的字符
print ("(c[0:-1]):" ,(c[0:-1]))
#输出第一个字符
print ("(c[0]):" ,(c[0]))
#输出c 索引[1]-[6]之间的字符
print ("(c[1:6]):", (c[1:6])) #Python中的字符串有两种索引方式,从左往右以0开始,从右往左以-1开始
#输出 索引[-1]-[-4]之间的字符
print ("(c[-1:-4]):", (c[-1:-4]))
#输出 索引[-4]-[-1]之间的字符
print ("(c[-4:-1]):", (c[-4:-1]))
#输出索引[2]以后的字符
print ("(c[2:]):", (c[2:]))
#输出2次c
print ("(c*2):", (c*2))
#输出两个字符串链接后的结果
print ('(c+"WWW"):', (c+"WWW")) print ("*"*60)
list1=['sub',2020,'sub2020',20.20,'http']
list2=['www',[1,2,3]] print ("list1 :" ,list1)
print ("list2 :" ,list2)
print ("len(list1) :" , len(list1))
print ("len(list2) :", len(list2))
print ("(list1[0:4]) :", (list1[0:4]))
# print ("(list1[-1]) :" (list1[-1])) 输出错误,list无法用负值索引
print ("list2*2 :", list2*2)
print ("list1+list2 :", list1+list2) #List中的元素是可以改变的
list1[4]="new"
print ("new list1 :" ,list1) print ("*"*60)
tuple1=('sub',2020,'sub2020',20.20,'http')
tuple2=('www',[1,2,3]) print ("tuple1 :" ,tuple1)
print ("tuple2 :" ,tuple2)
print ("len(tuple1) :" , len(tuple1))
print ("len(tuple2) :", len(tuple2))
print ("(tuple1[0:4]) :", (tuple1[0:4]))
#元组有两种索引方式,从左往右以0开始,从右往左以-1开始
print ("tuple1[-1] :" ,(tuple1[-1]))
print ("tuple2*2 :", tuple2*2)
print ("tuple1+tuple2 :", tuple1+tuple2) #tuple中的元素不可改变
#tuple1[4]="new"
#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]):', '')
('(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

最新文章

  1. 在js中添加新节点
  2. 探索javascript----拖拽
  3. Oracle中的sql操作符 和分析函数
  4. shell正则表达式(zhuan)
  5. Oracle常用命令
  6. nohup命令浅析
  7. 转:C++的重载(overload)与重写(override)
  8. 【Android 界面效果31】Android--侧滑菜单应用的实现
  9. 深入理解Java虚拟机 - Java体系
  10. Linux内核中的通用双向循环链表
  11. 20155304田宜楠 2006-2007-2 《Java程序设计》第二周学习总结
  12. AJAX验证码检查
  13. 将JavaScript转化为C#
  14. jQuery(五)、筛选
  15. [ZZ]新手学 appium-合集第一季度
  16. Python快速学习02:基本数据类型 &amp; 序列
  17. 用STM32CudeMX 配置用到的函数(记住他!)
  18. jdk1.7安装,cmd下 java -version出现错误:“could not open `D:\Java\jre7\lib\amd64\jvm.cfg”
  19. mysql 在linux下的启动
  20. k8s之服务发现

热门文章

  1. 通过lua扩展nginx
  2. linux之crontab定时器
  3. STL sort实现可迭代容器中对象的多重标准排序
  4. 微信小程序-饮食日志_开发记录02
  5. JAVA笔记26-网络编程(不等于网站编程)
  6. 【shell】grep使用正则表达式
  7. Type.GetType反射的对象创建Activator.CreateInstance
  8. new 做了什么
  9. 洛谷 P1282 多米诺骨牌 ( 线性DP )
  10. BZOJ 4422 Cow Confinement (线段树、DP、扫描线、差分)