你也许会问,为什么终止支持了,还会有公司再使用?其实,版本的更换是一项庞大、复杂的工作,一些小型的互联网公司在人力、财力不足的情况下,只能要继续使用低版本的 Python,只有万不得已时才会更新版本。
和 Python 2.x 版本相比,Python 3.x 版本在语句输出、编码、运算和异常等方面做出了一些调整,本节就对这些调整逐一地做简单介绍。
>>> print "3,4"
3,4
或者
>>> print(3,4)
(3,4)
>>> print(3,4)
3 4
>>> print "3,4"
File "<stdin>", line 1
print "3,4"
^
SyntaxError: Missing parentheses in call to 'print'
#Python 2.x
>>>str ="C语言中文网"
>>>str
'C\xe8\xaf\xad\xe8\xa8\x80\xe4\xb8\xad\xe6\x96\x87\xe7\xbd\x91'
#Python 3.x
>>>str ="C语言中文网"
>>>str
'C语言中文网'
>>>中国="China"
>>>print(中国)
China
>>>1/2
0
>>>1.0/2
0.5
>>>1/2
0.5
#Python 2.x
>>> -1//2
-1
#Python 3.x
>>> -1//2
-1
#Python 2.x
>>> try:
... raise TypeError,"类型错误"
... except TypeError,err:
... print err.message
...
类型错误
#Python 3.x
>>> try:
... raise TypeError("类型错误")
... except TypeError as err:
... print(err)
...
类型错误
#Python 2.x
>>> 0o1000
512
>>> 01000
512
#Python 3.x
>>> 01000
File "<stdin>", line 1
01000
^
SyntaxError: invalid token
>>> 0o1000
512
#Python 2.x
>>> 1!=2
True
>>> 1<>2
True
#Python 3.x
>>> 1!=2
True
>>> 1<>2
File "<stdin>", line 1
1<>2
^
SyntaxError: invalid syntax
Python 2.x
a=input("请输出:")
请输出:123
>>> type(a)
<type 'int'>
b=input("请输入")
请输出:"C语言中文网"
>>> type(b)
<type 'str'>
c=raw_input("请输入:")
请输入:123
>>>type(c)
<type 'str'>
# Python3.x
>>> d=input("请输入:")
请输入:123
>>> d
'123'
>>> type(d)
<class 'str'>
>>>b=b'China'
>>>type(b)
<type 'bytes'>
>>>s=b.decode()
>>>s
'China'
>>>b1=s.encode()
>>>b1
b'China'
本节所介绍的只是 Python 3.x 的一部分改动,由于篇幅有限,这里不再具体指出,教程中涉及到时再给大家详细介绍。
版权说明:Copyright © 广州松河信息科技有限公司 2005-2025 版权所有 粤ICP备16019765号
广州松河信息科技有限公司 版权所有