Python进阶之路---1.2python版本差异
2015-12-28 16:58
387 查看
Python2.*与python3.*版本差异
作为一个初学者,我们应该如何选择python的版本进行学习呢,这两个版本有什么区别呢,接下来让我们简单了解一下,以便我们后续的学习。[b]Python版本差异简介[/b]使用_future_模块Print函数整数除法UnicodeXrange触发异常处理异常next()函数和.net()方法for循环变量与全局命名空间泄露比较无序类型使用input()解析输入内容返回可迭代对象,而不是列表使用_future_模块Python3.x引入了一些与Python2不兼容的关键字和特性,在Python2中,可以通过内置的__future__模块导入这些新内容。如果你希望在Python2环境下写的代码也可以在Python3.x中运行,那么建议使用__future__模块。例如,如果希望在Python2中拥有Python3.x的整数除法行为,可以通过下面的语句导入相应的模块。from__future__importdivision下表列出了__future__中其他可导入的特性:特性 | 可选版本 | 强制版本 | 效果 |
nested_scopes | 2.1.0b1 | 2.2 | |
generators | 2.2.0a1 | 2.3 | |
division | 2.2.0a2 | 3.0 | |
absolute_import | 2.5.0a1 | 3.0 | |
with_statement | 2.5.0a1 | 2.6 | |
print_function | 2.6.0a2 | 3.0 | |
unicode_literals | 2.6.0a2 | 3.0 |
fromplatformimportpython_version
print函数
虽然print语法是Python3中一个很小的改动,且应该已经广为人知,但依然值得提一下:Python2中的print语句被Python3中的print()函数取代,这意味着在Python3中必须用括号将需要输出的对象括起来。 在Python2中使用额外的括号也是可以的。但反过来在Python3中想以Python2的形式不带括号调用print函数时,会触发SyntaxError。Python2print"helloword"Python3
print('Hello,World!')print("sometext,",end="")print('printmoretextonthesameline')注意:在Python中,带不带括号输出"HelloWorld"都很正常。但如果在圆括号中同时输出多个对象时,就会创建一个元组,这是因为在Python2中,print是一个语句,而不是函数调用。
整数除法
由于人们常常会忽视Python3在整数除法上的改动(写错了也不会触发SyntaxError),所以在移植代码或在Python2中执行Python3的代码时,需要特别注意这个改动。所以,我还是会在Python3的脚本中尝试用float(3)/2或3/2.0代替3/2,以此来避免代码在Python2环境下可能导致的错误(或与之相反,在Python2脚本中用from__future__importdivision来使用Python3的除法)。Python2print'3/2=',3/2print'3//2=',3//2print'3/2.0=',3/2.0print'3//2.0=',3//2.03/2=13//2=13/2.0=1.53//2.0=1.0Python3
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.53//2=13/2.0=1.53//2.0=1.0
unicode
Python2有基于ASCII的str()类型,其可通过单独的unicode()函数转成unicode类型,但没有byte类型。而在Python3中,终于有了Unicode(utf-8)字符串,以及两个字节类:bytes和bytearrays。Python2
printtype(unicode('thisislikeapython3strtype'))<type'unicode'>printtype(b'bytetypedoesnotexist')<type'str'>print'theyarereally'+b'thesame'theyarereallythesameprinttype(bytearray(b'bytearrayoddlydoesexistthough'))<type'bytearray'>
Python3
print('has',type(b'bytesforstoringdata'))has<class'bytes'>print('alsohas',type(bytearray(b'bytearrays')))alsohas<class'bytearray'>
xrange
在Python2中xrange()创建迭代对象的用法是非常流行的。比如:for循环或者是列表/集合/字典推导式。这个表现十分像生成器(比如。"惰性求值")。但是这个xrange-iterable是无穷的,意味着你可以无限遍历。由于它的惰性求值,如果你不得仅仅不遍历它一次,xrange()函数比range()更快(比如for循环)。尽管如此,对比迭代一次,不建议你重复迭代多次,因为生成器每次都从头开始。在Python3中,range()是像xrange()那样实现以至于一个专门的xrange()函数都不再存在(在Python3中xrange()会抛出命名异常)。importtimeitn=10000deftest_range(n):returnforiinrange(n):passdeftest_xrange(n):foriinxrange(n):passpython2
print'Python',python_version()print'\ntimingrange()'%timeittest_range(n)print'\n\ntimingxrange()'%timeittest_xrange(n)Python2.7.6timingrange()1000loops,bestof3:433µsperlooptimingxrange()1000loops,bestof3:350µsperlooppython3
print('Python',python_version())print('\ntimingrange()')%timeittest_range(n)Python3.4.1timingrange()1000loops,bestof3:520µsperloop
print(xrange(10))---------------------------------------------------------------------------NameErrorTraceback(mostrecentcalllast)<ipython-input-5-5d8f9b79ea70>in<module>()---->1print(xrange(10))NameError:name'xrange'isnotdefined
八进制字面量表示
八进制数必须写成0o777,原来的形式0777不能用了;二进制必须写成0b111。新增了一个bin()函数用于将一个整数转换成二进制字串。Python2.6已经支持这两种语法。在Python3.x中,表示八进制字面量的方式只有一种,就是0o1000。python2x>>>0o1000512>>>01000512python3x
>>>01000File"<stdin>",line101000^SyntaxError:invalidtoken>>>0o1000512
不等运算符
Python2.x中不等于有两种写法!=和<>Python3.x中去掉了<>,只有!=一种写法。去掉了repr表达式``
Python2.x中反引号``相当于repr函数的作用Python3.x中去掉了``这种写法,只允许使用repr函数,这样做的目的是为了使代码看上去更清晰么?不过我感觉用repr的机会很少,一般只在debug的时候才用,多数时候还是用str函数来用字符串描述对象。defsendMail(from_:str,to:str,title:str,body:str)->bool:pass
多个模块被改名(根据PEP8)
旧的名字 | 新的名字 |
---|---|
_winreg | winreg |
ConfigParser | configparser |
copy_reg | copyreg |
Queue | queue |
SocketServer | socketserver |
repr | reprlib |
数据类型
1)Py3.X去除了long类型,现在只有一种整型——int,但它的行为就像2.X版本的long2)新增了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()>>>b1b'china'3)dict的.keys()、.items和.values()方法返回迭代器,而之前的iterkeys()等函数都被废弃。同时去掉的还有dict.has_key(),用in替代它吧。
相关文章推荐
- Python编辑器安装
- Python安装setuptools的脚本
- Python 安装matplotlib,six,dateutil,pyparsing 完整过程
- Python文件写到本地
- MFC 使用Python脚本语言
- 【python】配置文件
- python入门到精通[一]:搭建开发环境
- Python 3.0中ACCESS学习(三) 打开记录集并输出数据
- python读取表格csv的方法
- python连接数据库出报表
- 解读python中SocketServer源码
- 读书笔记--python数据可视化--006_从数据库导入数据
- 读书笔记--python数据可视化--005_从数据库导入数据
- day①:python的变量及数据类型
- Python 遍历文件,字符串操作
- Python IDLE快捷键 汇总
- selenium+python 遍历select下拉框获取value值
- python 代码格式化工具:pep8ify
- 排序算法—归并排序算法分析与实现(Python)
- Python标准库05 存储对象 (pickle包,cPickle包)