Python2 和Python3的区别汇总

1.打印信息:
python2: print "hello world"
python3: print ("hello world")
2.除法运算:
Python 2 :print '3 / 2 =', 3 / 2 输出:3 / 2 = 1
print '3 / 2.0 =', 3 / 2.0 输出:3 / 2.0 = 1.5
Python 3: print('3 / 2 =', 3 / 2) 输出: 3 / 2 = 1.5
print('3 / 2.0 =', 3 / 2.0) 输出: 3 / 2.0 = 1.5
3.Unicode
Python 2有两种字符串类型:str和unicode,
Python 3中的字符串默认就是Unicode,Python 3中的str相当于Python 2中的unicode。
在Python 2中,如果代码中包含非英文字符,需要在代码文件的最开始声明编码,如下 # -- coding: utf-8 --
在Python 3中,默认的字符串就是Unicode,就省去了这个麻烦,下面的代码在Python 3可以正常地运行
a = "你好" print(a)

其他差异后续补充。。。。。。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容