版本说明
Python 3.0 在设计的时候没有考虑向较早版本相容
Python 2.6 作为一个过渡版本, 基本使用了 Python 2.x 的语法和库, 同时考虑了向 Python 3.0 的迁移, 允许使用部分 Python 3.0 的语法与函数.
除非为了使用旧的 Python2.x 项目代码或只支持 2.x 的第三方库, 否则不推荐使用 2.x 进行编程
死人的 print 函数
Python 2.6 与 Python 2.7 里面, 以下三种形式是等价的:
- print "fish"
- print ("fish") #注意 print 后面有个空格
- print("fish") #print()不能带有任何其它参数
但 python3.x 只能使用后两者, print 语句被 python3 废弃, 只能使用 print 函数
Unicode
Python3 中字符串是 Unicode (utf-8)编码, 支持中文做标识符.
python2 中是 ASCII 编码, 需要更改字符集才能正常支持中文, 所以在. py 文件中会看到 #-- coding: UTF-8 --.
#python3 中
>>> 中国 = 'china'
- >>>print(中国)
- china
- #python2 中
- >>> str = "我爱北京天安门"
- >>> str
- '\xe6\x88\x91\xe7\x88\xb1\xe5\x8c\x97\xe4\xba\xac\xe5\xa4\xa9\xe5\xae\x89\xe9\x97\xa8'
- >>> str = u"我爱北京天安门"
- >>> str
- u'\u6211\u7231\u5317\u4eac\u5929\u5b89\u95e8'
除法运算
单斜杠 /,Python2 中整数相除得整数, 浮点小数相除得浮点; Python3 中结果总是浮点数.
- #python3
- >>print(10/5)
- 2.0
双斜杠 //,Python2 和 3 相同, 都是除法结果去掉小数部分
>>print(10//3) 3
异常处理
Python2 中 try:...except ERR,e:..., 在 Python3 中改为了 try:...except ERR as e:...
- #Python3
- try:
- open('a.txt','r')
- except Exception as e:
- print(e) #这里也不要使用 e.message
python 2 中触发异常可以用 raise IOError, "file error" 或 raise IOError("file error")两种方式.
python 3 中触发异常只能用 raise IOError("file error").
异常 StandardError 被 Python3 废弃, 统一使用 Exception
xrange 和 range
Python3 中不再使用 xrange 方法, 只有 range 方法
range 在 Python2 中返回列表, 而在 Python3 中返回 range 可迭代对象
- a=range(10)
- print(a)
- print(list(a))
输出
- range(0, 10)
- [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
八进制字面量
Python3 中只能使用 0o... 格式, 对于 01000 格式将抛出错误, 而在 Python2 中两种都能使用
- >>> 01000
- File "<stdin>", line 1
- 01000
- ^
- SyntaxError: invalid token
- >>> 0o1000
- 512
不等运算符
在 Python2 中有两个不等运算符!= 和<>, 在 Python3 中去掉了<>, 只有!= 符号表示不等.
repr
在 Python2 中双反引号 `` 可以替代 repr 函数, 在 Python3 中去掉了双反引号的表是方法, 只能用 repr 方法
模块改名
StringIO 模块现在被合并到新的 io 模组内. new, md5, gopherlib 等模块被删除.
httplib, BaseHTTPServer, CGIHTTPServer, SimpleHTTPServer, Cookie, cookielib 被合并到 http 包内.
取消了 exec 语句, 只剩下 exec()函数.
long 类型
在 Python2 中 long 是比 int 取值范围更大的整数, Python3 中取消了 long 类型, int 的取值范围扩大到之前的 long 类型范围
bytes 类型
Python3 新增了 bytes 类型, 使用 b 开头的字符串定义:
- >>> b = b'china'
- >>> type(b)
- <type 'bytes'>
str 对象和 bytes 对象可以使用. encode() (str -> bytes) or .decode() (bytes -> str)方法相互转化.
- >>> s = b.decode()
- >>> s
- 'china'
- >>> b1 = s.encode()
- >>> b1
- b'china'
dict 类型
Python3 中 dict 的. keys(),.items 和. values()方法返回迭代器, 而之前的 iterkeys()等函数都被废弃.
同时去掉的还有 dict.has_key(), 可以用 in 来代替.
- di={
- 'a':1,
- 'b':2,
- 'c':3
- }
- for item in d.items():
- print(item)
- print('c' in di)
输出
- ('gggg', {
- 'a1': 1
- })
- ('b', 12)
- True
next()函数和. next()方法
my_generator = (letter for letter in 'abcdefg')
python 2 中可以用 next(my_generator) 和 my_generator.next() 两种方式.
python 3 中只能用 next(my_generator)这种方式.
列表推导
不再支持 [n for n in a,b] 语法, 改为 [n for n in (a,b)] 或[n for n in [a,b]]
- a=1
- b=2
- c=[n for n in [a,b]]
- print(c)
输出[1,2]
input
python 2 中通过 input 输入的类型是 int, 只有通过 raw_input()输入的类型才是 str.
python 3 中通过 input 输入的类型都是是 str, 去掉了 row_input()方法.
比较符
Python 2 中任意两个对象都可以比较, 11 < 'test'返回 True
Python 3 中只有同一数据类型的对象可以比较, 11 < 'test'报错, 需要调用正则判断, 改为 import re;11 < int('test') if re.compile('^[0-9]+$').match('test') else 0 否则就报错
其他
exec 语句被 python3 废弃, 统一使用 exec 函数
execfile 语句被 Python3 废弃, 推荐使用 exec(open("./filename").read())
Python3 中这些方法不再返回 list 对象: dictionary 关联的 keys(),values(),items(),zip(),map(),filter(), 但是可以通过 list 强行转换
迭代器 iterator 的 next()函数被 Python3 废弃, 统一使用 next(iterator)
file 函数被 Python3 废弃, 统一使用 open 来处理文件, 可以通过 io.IOBase 检查文件类型
apply 函数被 Python3 废弃
更多内容请你在实践中慢慢体会.
来源: http://www.jianshu.com/p/2923b60f48f5