python2 和3的区别
__future__ 模块
Python 3.x引入一些Python2不兼容的关键字和函数,可以通过在 Python2 内置的模块 __future__ 导入。建议如果你想在代码中支持 Python3.x,使用__future__导入它。
from __future__ import division
print函数
在 Python3 最值得注意和最广为人知的变化是print函数的使用。print 函数使用的括号()在Python3中是强制性的。它在 Python2 中是可选的。
print "Hello World" #is acceptable in Python 2
print ("Hello World") # in Python 3, print must be followed by ()
print()函数默认情况下在结束时会插入一个换行。在 Python2,它可以通过 ',' 在末行抑制输出换行。 在 Python3 则使用"end=' '" 附加空格,而不是换行
print x, # Trailing comma suppresses newline in Python 2
print(x, end=" ") # Appends a space instead of a newline in Python 3
Python2 中有输入函数两个版本。 input() 和 raw_input()。如果它被包含在引号 '' 或 "",input() 对待接收到的数据作为字符串,否则数据将被视为数字类型。
In Python 2 >>> x=input('something:')
something:10 #entered data is treated as number
>>> x
10
>>> x=input('something:')
something:'10' #eentered data is treated as string
>>> x
'10'
>>> x=raw_input("something:")
something:10 #entered data is treated as string even without ''
>>> x
'10'
>>> x=raw_input("something:")
something:'10' #entered data treated as string including ''
>>> x
"'10'" In Python 3 >>> x=input("something:")
something:10
>>> x
'10'
>>> x=input("something:")
something:'10' #entered data treated as string with or without ''
>>> x
"'10'"
>>> x=raw_input("something:") # will result NameError
Traceback (most recent call last):
File "", line 1, in x=raw_input("something:")
NameError: name 'raw_input' is not defined
在Python2,两个整数的除法的结果会四舍五入到最接近的整数。如:3/2 其结果将显示 1。 为了获得一个浮点除法,分子或分母必须明确为浮点数。因此无论是 3.0/2 或 3/2.0 或 3.0/2.0 将产生1.5 。
Python3 中的字符串默认存储为 Unicode。在Python3,我们有个Unicode(UTF-8)字符串和 2 字节类:字节和字节数组。
在Python3,range()函数去除了,而 xrange()已更名为 range()。 另外在 Python3.2 以及更高的版本中, range()对象支持切片。
Python2 中同时接受符号的'大胆'和'新'的语法;如果我们不在括号中括入异常参数,Python3 中会引发一个 SyntaxError:
raise IOError, "file error" #This is accepted in Python 2
raise IOError("file error") #This is also accepted in Python 2
raise IOError, "file error" #syntax error is raised in Python 3
raise IOError("file error") #this is the recommended syntax in Python 3
except Myerror, err: # In Python2
except Myerror as err: #In Python 3
next() 函数和.next()方法
在Python 2,next() 作为生成器对象的一个方法是允许的。在 Python2,next()函数过度产生器对象遍历也是可以接受的。在Python3,但是,next()函数作为生成器方法来中止并引发AttributeError。
gen = (letter for letter in 'Hello World') # creates generator object next(my_generator) #allowed in Python 2 and Python 3
my_generator.next() #allowed in Python 2. raises AttributeError in Python 3
2to3实用工具
随着 Python3 解释器,2t03.py 脚本将被通常安装在 tools/scripts 文件夹。 它读取 Python2.x 源代码,并应用了一系列的修复将它转变成有效的 Python3.x 代码。
Here is a sample Python 2 code (area.py): def area(x,y=3.14):
a=y*x*x
print a
return a a=area(10)
print "area",a To convert into Python 3 version: $2to3 -w area.py Converted code : def area(x,y=3.14): # formal parameters
a=y*x*x
print (a)
return a a=area(10)
print("area",a)
python2 和3的区别的更多相关文章
- 详解python2 和 python3的区别
看到这个题目大家可能猜到了我接下来要讲些什么,呵呵,对了,那就是列出这两个不同版本间的却别!搜索一下大家就会知道,python有两个主要的版本,python2 和 python3 ,但是python又 ...
- python2与python3的区别 ,小数据池 bytes 类型
一.python2和3的区别 在python3中 在python2中 print('ab')方式打印内容()括号是必须要有的. print 'ab' 可以加可以不加. 只有range 有ran ...
- python2和python3的区别
python2和python3的区别 参考链接:http://www.runoob.com/python/python-2x-3x.html 1.源码上的区别 python2 python3 源码不规 ...
- python2 与 python3的区别
python2 与 python3的区别 几乎所有的python2程序都需要一些修改才能正常的运行在python3的环境下.为了简化这个转换过程,Python3自带了一个2to3的实用脚本.这个脚本会 ...
- while 运算符 初始编码 python2和python3的区别
1.while 循环 2.运算符 3.初始编码 4.python2 和python3的区别 1.while循环: 关键词:while[循环] break[跳出循环] c ...
- python2 与 python3 语法区别
python2 与 python3 语法区别 概述# 原稿地址:使用 2to3 将代码移植到 Python 3 几乎所有的Python 2程序都需要一些修改才能正常地运行在Python 3的环境下.为 ...
- python2 与 python3的区别总结
python2 与 python3的区别总结 几乎所有的Python 2程序都需要一些修改才能正常地运行在Python 3的环境下.为了简化这个转换过程,Python 3自带了一个叫做2to3的 ...
- 详解python2 和 python3的区别-乾颐堂
看到这个题目大家可能猜到了我接下来要讲些什么,呵呵,对了,那就是列出这两个不同版本间的却别!搜索一下大家就会知道,python有两个主要的版本,python2 和 python3 ,但是python又 ...
- python2和3的区别丶网络编程以及socketserver多线程
一丶python2和python3的区别 1.编码&字符串 字符串: python2: Unicode v = u"root" 本质上用unicode存储(万国码) (s ...
- python2与python3的区别,以及注释、变量、常量与编码发展
python2与python3的区别 宏观上: python2:源码不统一,混乱,重复代码太多. python3:源码统一标准,能去除重复代码. 编码上: python2:默认编码方式为ASCII码. ...
随机推荐
- pyqt QTableView例子学习
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' from PyQt4.QtGui import * from Py ...
- Hibernate框架大配置关联小配置
1 <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-// ...
- Hibernate(四)——缓存策略+lazy
Hibernate作为和数据库数据打交道的框架,自然会设计到操作数据的效率问题,而对于一些频繁操作的数据,缓存策略就是提高其性能一种重要手段,而Hibernate框架是支持缓存的,而且支持一级和二级两 ...
- (转)25个增强iOS应用程序性能的提示和技巧--中级篇
在性能优化时,当你碰到一些复杂的问题,应该注意和使用如下技巧: 9.重用和延迟加载View10.缓存.缓存.缓存11.考虑绘制12.处理内存警告13.重用花销很大的对象14.使用Sprite Shee ...
- Android学习总结——欢迎页和导航页的实现
activity_welcome.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayo ...
- Timer.3 - Binding arguments to a handler
In this tutorial we will modify the program from tutorial Timer.2 so that the timer fires once a sec ...
- OutputCache各参数的说明
OutputCache各参数的说明 Duration 缓存时间,以秒为单位,这个除非你的Location=None,可以不添加此属性,其余时候都是必须的. Location Location当被设置为 ...
- T4模板_根据DB生成实体类
为了减少重复劳动,可以通过T4读取数据库表结构,生成实体类,用下面的实例测试了一下 1.首先创建一个项目,并添加文本模板: 2.添加 文本模板: 3.向T4文本模板文件添加代码: <#@ tem ...
- iOS archive(归档)的总结
http://www.cnblogs.com/ios8/p/ios-archive.html
- OD调试1--第一个win32程序
OD调试一:第一个Win32程序的修改 在软件开发的过程中,程序员会使用一些调试工具,以便高效地找出软件中存在的错误.而在逆向分析领域,分析者也会利用相关的调试工具来分析软件的行为并验证分析结果.由于 ...