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__ import division

下表列出了__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

    
    from platform import python_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'))

<type 'unicode'>
print type(b'byte type does not exist') <type 'str'>
print 'they are really' + b' the same' they are really the same
print type(bytearray(b'bytearray oddly does exist though')) <type 'bytearray'>

Python3

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

has <class 'bytes'>

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

also has <class 'bytearray'>

xrange

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

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

 
import timeit

n = 10000
def test_range(n):
return for i in range(n):
pass def test_xrange(n):
for i in xrange(n):
pass

  python2

print 'Python', python_version()

print '\ntiming range()'
%timeit test_range(n) print '\n\ntiming xrange()'
%timeit test_xrange(n) Python 2.7.6 timing 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) Python 3.4.1 timing range()
1000 loops, best of 3: 520 µs per loop
print(xrange(10))
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-5-5d8f9b79ea70> in <module>()
----> 1 print(xrange(10)) NameError: name 'xrange' is not defined

八进制字面量表示

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

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

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

python 2x

>>> 0o1000
512
>>> 01000
512

python 3x

>>> 01000
File "<stdin>", line 1
01000
^
SyntaxError: invalid token
>>> 0o1000
512

不等运算符

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)
<type 'bytes'>

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替代它吧 。

Python进阶之路---1.2python版本差异的更多相关文章

  1. Python进阶之路---1.4python数据类型-数字

    python入门基础 声明:以后python代码未注明情况下,默认使用python3.x版本 1.python代码基础:print     print('hello,python')   1.1pyt ...

  2. Python进阶之路---1.3python环境搭建

      python环境安装 windows python环境安装 下载安装包     https://www.python.org/downloads/ 安装并指定安装目录     C:\python2 ...

  3. Python进阶之路---1.5python数据类型-字符串

    字符串 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; ...

  4. python进阶之路之文件处理

    Python之文件处理 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !imp ...

  5. python进阶之路4.2---装饰器

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  6. Python进阶之路---1.1python简介

                            Python简介 Python简介 Python (发音:[ 'paiθ(ə)n; (US) 'paiθɔn ]n.蟒蛇,巨蛇 ),是一种面向对象的解释 ...

  7. python进阶之路4.1---生成器与迭代器

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  8. python从零开始 -- 第2篇之python版本差异

    python从零开始 -- 第2篇之python版本差异 第0篇开始,咱们就说选择 python 3.x,一般来说,咱们面临选择的时候总是想了解更多一点,并且版本之间的对比能引申出很多有意思的故事和知 ...

  9. 【Python高级工程师之路】入门+进阶+实战+爬虫+数据分析整套教程

    点击了解更多Python课程>>> 全网最新最全python高级工程师全套视频教程学完月薪平均2万 什么是Python? Python是一门面向对象的编程语言,它相对于其他语言,更加 ...

随机推荐

  1. mybatis的简单使用

    使用mybatis数据库时,需要添加一下jar包: asm-3.3.1.jarcglib-2.2.2.jarjavassist-3.17.1-GA.jarlog4j-1.2.17.jarmybatis ...

  2. css中的clear的正真意义

    网上包括w3cschool 的说法都是 不允许浮动.消除浮动 这样的说法,却看了还是不知道什么意思,一些地方说不通. 所以找到w3c css 的英文文档,如下是节选: 'clear'Values ha ...

  3. MVC文件上传 - 使用Request.Files上传多个文件

    控制器 1: using System; 2: using System.Collections.Generic; 3: using System.IO; 4: using System.Linq; ...

  4. Path对象

    Path是连续的Segment的集合,除了 Path 的第一个Segment和最后一个Segment外,其余的Segment的起始点都是前一个Segment的终止点,即Path对象的中的Segment ...

  5. 常见Oracle数据库问题总结及解决办法(一)

    开发中常使用Oralce数据库,使用中也许会碰到形形色色的各类错误提示,如:ORA-00933:SQL命令未正确结束.ORA-009242等等,为此记录积累对于自己来说还是很有帮助的,今天就记录以前出 ...

  6. 修改Servlet模板

    1.找到jar文件 查看MyEclipse根目录下的myeclipse.ini,找到Common文件夹的位置,打开文件夹..\Common\plugins 找到文件 com.genuitec.ecli ...

  7. POJ3484 Showstopper (二分+字符串处理)

    POJ3484 Showstopper 题目大意: 每次给出三个数x,y,z,用这三个数构成一个等差数列,x为首项,y是末项,z是公差 总共给出n组x,y,z( n待定),求这n组数列中出现次数为奇数 ...

  8. HTML&CSS基础学习笔记1.6-html的文本操作标签

    文本也许是HTML里最常见的元素了,所以我们有必要对HTML的文本操作标签做下认识. 1. <em>,<i>内的文字呈现为倾斜效果: 2. <strong>,< ...

  9. [工具]web开发时自动刷新网页:liveReload

    传统网页开发流程:用sublime text写好代码,运行,发现问题,再回到sublime text修改,运行…如此往复,十分繁琐.今天看到有人(<LiveReload>讓Sublime ...

  10. windows进程间通讯的方法

    版权声明 请尊重原创作品.转载请保持文章完整性,并以超链接形式注明原始作者“tingsking18”和主站点地址,方便其他朋友提问和指正.   1.使用共享内存 代码如下: void FileMapp ...