python2.x和python3.x的区别

来源:互联网 发布:深圳软件产业基地租房 编辑:程序博客网 时间:2024/06/02 05:12

刚刚开始学习python,本文章用来记录下python2.x和python3.x遇到的不同之处。
我是基于廖学峰的python2.7教程学习的,但我使用的是python3.6.2版本,https://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000


1、输入
python2.x:

print 'hello world'

python3.x:

print ("hello world")

2、输出
python2.x:

raw_input()

python3.x:

input() 

3、比较运算
python2.x:
(1)字符串和数字可以比较,例 ‘1982’ < 2000。
(2)可使用cmp(a,b)函数。
python3.x:
(1)报错,SyntaxError: invalid character in identifier。
(2)cmp()函数取消,如实在需要可用(a > b) - (a < b)替换。

4、强制转换成字符串
python2.x:
有两种字符串类型:Unicode字符串和非Unicode字符串。
unicode()把对象转换成Unicode字符串,还有str()把对象转换为非Unicode字符串。
python3.x:
只有一种类型:Unicode字符串(Unicode strings)。
str()函数即可完成所有的功能,转换成Unicode字符串。

原创粉丝点击