python版本差异_Python进阶之路---1.2python版本差异

Python2.*与python3.*版本差异

作为一个初学者,我们应该如何选择python的版本进行学习呢,这两个版本有什么区别呢,接下来让我们简单了解一下,以便我们后续的学习。

Python版本差异简介

使用_future_模块

Print函数

整数除法

Unicode

Xrange

触发异常

处理异常

next()函数和.net()方法

for循环变量与全局命名空间泄露

比较无序类型

使用input()解析输入内容

返回可迭代对象,而不是列表

使用_future_模块

Python 3.x引入了一些与Python 2不兼容的关键字和特性,在Python 2中,可以通过内置的__future__模块导入这些新内容。如果你希望在Python 2环境下写的代码也可以在Python 3.x中运行,那么建议使用__future__模块。例如,如果希望在Python 2中拥有Python 3.x的整数除法行为,可以通过下面的语句导入相应的模块。

from__future__ importdivision

下表列出了__future__中其他可导入的特性:

特性

可选版本

强制版本

效果

nested_scopes

2.1.0b1

2.2

PEP 227:

Statically Nested Scopes

generators

2.2.0a1

2.3

PEP 255:

Simple Generators

division

2.2.0a2

3.0

PEP 238:

Changing the Division Operator

absolute_import

2.5.0a1

3.0

PEP 328:

Imports: Multi-Line and Absolute/Relative

with_statement

2.5.0a1

2.6

PEP 343:

The "with" Statement

print_function

2.6.0a2

3.0

PEP 3105:

Make print a function

unicode_literals

2.6.0a2

3.0

PEP 3112:

Bytes literals in Python 3000

fromplatform importpython_version

print函数

虽然print语法是Python 3中一个很小的改动,且应该已经广为人知,但依然值得提一下:Python 2中的print语句被Python 3中的print()函数取代,这意味着在Python 3中必须用括号将需要输出的对象括起来。

在Python 2中使用额外的括号也是可以的。但反过来在Python 3中想以Python2的形式不带括号调用print函数时,会触发SyntaxError。

Python2

print "hello word"

Python3

print('Hello, World!')print("some text,", end="")print('print more text on the same line')

注意:

在Python中,带不带括号输出"Hello World"都很正常。但如果在圆括号中同时输出多个对象时,就会创建一个元组,这是因为在Python 2中,print是一个语句,而不是函数调用。

整数除法

由于人们常常会忽视Python 3在整数除法上的改动(写错了也不会触发Syntax Error),所以在移植代码或在Python 2中执行Python 3的代码时,需要特别注意这个改动。

所以,我还是会在Python 3的脚本中尝试用float(3)/2或3/2.0代替3/2,以此来避免代码在Python 2环境下可能导致的错误(或与之相反,在Python 2脚本中用from __future__ import division来使用Python 3的除法)。

Python2

print '3 / 2 =', 3 / 2

print '3 // 2 =', 3 // 2

print '3 / 2.0 =', 3 / 2.0

print '3 // 2.0 =', 3 // 2.0

3 / 2 = 1

3 // 2 = 1

3 / 2.0 = 1.5

3 // 2.0 = 1.0

Python3

print('3 / 2 =', 3 / 2)print('3 // 2 =', 3 // 2)print('3 / 2.0 =', 3 / 2.0)print('3 // 2.0 =', 3 // 2.0)3 / 2 = 1.5

3 // 2 = 1

3 / 2.0 = 1.5

3 // 2.0 = 1.0

unicode

Python 2有基于ASCII的str()类型,其可通过单独的unicode()函数转成unicode类型,但没有byte类型。

而在Python 3中,终于有了Unicode(utf-8)字符串,以及两个字节类:bytes和bytearrays。

Python2

print type(unicode('this is like a python3 str type'))

print type(b'byte type does not exist')

print 'they are really' + b'the same'they are really the sameprint type(bytearray(b'bytearray oddly does exist though'))

Python3

print('has', type(b'bytes for storing data'))

has

print('also has', type(bytearray(b'bytearrays')))

also has

xrange

在 Python 2 中 xrange() 创建迭代对象的用法是非常流行的。比如: for 循环或者是列表/集合/字典推导式。这个表现十分像生成器(比如。"惰性求值")。但是这个 xrange-iterable 是无穷的,意味着你可以无限遍历。由于它的惰性求值,如果你不得仅仅不遍历它一次,xrange() 函数 比 range() 更快(比如 for 循环)。尽管如此,对比迭代一次,不建议你重复迭代多次,因为生成器每次都从头开始。

在 Python 3 中,range() 是像 xrange() 那样实现以至于一个专门的 xrange() 函数都不再存在(在 Python 3 中 xrange() 会抛出命名异常)。

importtimeit

n= 10000

deftest_range(n):return for i inrange(n):pass

deftest_xrange(n):for i inxrange(n):pass

python2

print 'Python', python_version()print '\ntiming range()'

%timeit test_range(n)print '\n\ntiming xrange()'

%timeit test_xrange(n)

Python2.7.6timing range()1000 loops, best of 3: 433µs per loop

timing xrange()1000 loops, best of 3: 350 µs per loop

python3

print('Python', python_version())print('\ntiming range()')%timeit test_range(n)

Python3.4.1timing range()1000 loops, best of 3: 520 µs per loop

print(xrange(10))---------------------------------------------------------------------------NameError Traceback (most recent call last) in ()----> 1 print(xrange(10))

NameError: name'xrange' is not defined

八进制字面量表示

八进制数必须写成0o777,原来的形式0777不能用了;二进制必须写成0b111。

新增了一个bin()函数用于将一个整数转换成二进制字串。 Python 2.6已经支持这两种语法。

在Python 3.x中,表示八进制字面量的方式只有一种,就是0o1000。

python 2x

>>>0o1000512

>>> 01000

512

python 3x

>>> 01000File"", line 1

01000

^SyntaxError: invalid token>>>0o1000512

不等运算符

Python 2.x中不等于有两种写法 != 和 <>

Python 3.x中去掉了<>, 只有!=一种写法。

去掉了repr表达式``

Python 2.x 中反引号``相当于repr函数的作用

Python 3.x 中去掉了``这种写法,只允许使用repr函数,这样做的目的是为了使代码看上去更清晰么?不过我感觉用repr的机会很少,一般只在debug的时候才用,多数时候还是用str函数来用字符串描述对象。

def sendMail(from_: str, to: str, title: str, body: str) ->bool:pass

多个模块被改名(根据PEP8)

旧的名字新的名字

_winreg

winreg

ConfigParser

configparser

copy_reg

copyreg

Queue

queue

SocketServer

socketserver

repr

reprlib

StringIO模块现在被合并到新的io模组内。 new, md5, gopherlib等模块被删除。Python 2.6已经支援新的io模组。

httplib, BaseHTTPServer, CGIHTTPServer, SimpleHTTPServer, Cookie, cookielib被合并到http包内。

取消了exec语句,只剩下exec()函数。 Python 2.6已经支援exec()函数。

数据类型

1)Py3.X去除了long类型,现在只有一种整型——int,但它的行为就像2.X版本的long

2)新增了bytes类型,对应于2.X版本的八位串,定义一个bytes字面量的方法如下:

>>> b = b'china'

>>>type(b)

str对象和bytes对象可以使用.encode() (str -> bytes) or .decode() (bytes -> str)方法相互转化。

>>> s =b.decode()>>>s'china'

>>> b1 =s.encode()>>>b1

b'china'

3)dict的.keys()、.items 和.values()方法返回迭代器,而之前的iterkeys()等函数都被废弃。同时去掉的还有 dict.has_key(),用 in替代它吧 。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值