python2和python3版本的区别(画上重点)

1、整数:python2中区分整型(int)和长整型(long),python3不区分;

2、python2中没有f 标志位格式化,python3才有;

3、python3里面,True和False都是关键字,python2里面不是;

4、python2中的整数相除属于地板除,要想真除就得变成浮点数
  python3中的整数相除就是真除

5、python2的编码是ascii码,python3的默认编码是utf8

若是要python2中要输入汉文的话,要在前边说明"encoding = utf-8"

6、包结构的目录里有一个——int_.py模块。py2,_init_.py是必须的;py3,_init_.py是可选的
比如:。这里,py2只会识别pack02是一个包,而py3都会识别成包

7、模块运行的时候。python2,只会在模块同级目录下生成.pyc文件;python3会生成__pycache__目录文件。

8、输出和输入

输出:

python2的print不一定要求函数形式使用,python2可以这样输出print"hello"

python3的print强制是函数,例如,print("hello")

输入:

python3中input得到的数据类型都是str

python2中的input默认是int,str要使用引号包裹,raw_input得到的数据类型都是str

Python 2.7.5 (default, Oct 14 2020, 14:45:30) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print "hello"
hello
>>> print ("hello")
hello
>>> 
KeyboardInterrupt


[root@fttsaxf ~]# python3
Python 3.6.8 (default, Nov 16 2020, 16:55:22) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print("hello")
hello

9、输入函数input的区别

python2里边原来的input被淘汰了(但是还能使用),变成了raw_input就是python3中的input

python2:

  • input()           #如果输入是一个数学计算式,会自动执行计算得出结果,并且会自动识别出输入的类型(str,int,float)
  • raw_input()    #完全默认为str类型

python3:

input(),默认就是str类型

10、range的区别

列表一定是可迭代对象,可迭代对象不一定是列表

range和xrange

python2,range(0,4) -->结果:[0,1,2,3];xrange(0,4) -->结果是一个可迭代对象,是一个惰性求值,即使用的时候再生成值

python3,没有xrange,python3的range函数就是python2的xrange

11、异常捕获差异:

python2,except Exception,e:

python3,   except Exception as e:

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FanMY_71

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值