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码. ...
随机推荐
- SQL 查找 45道练习题
一. 设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表( ...
- Linux Top 命令
TOP命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况. TOP是一个动态显示过程,即可以通过用户按键来不断刷新当前状态.如果在前台执行该命令,它将独占前台,直到用户终止 ...
- UITableView 自定义选中Cell颜色
cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame]; cell.selectedBackgroundView ...
- UIView 转 UIImage
这个方法很实用,特别是在做水印相机得时候... - (UIImage*) imageWithUIView:(UIView*) view{ // 创建一个bitmap的context // 并把它设置成 ...
- PC--CSS常识
1.不要使用过小的图片做背景平铺.这就是为何很多人都不用 1px 的原因,这才知晓.宽高 1px 的图片平铺出一个宽高 200px 的区域,需要 200*200=40, 000 次,占用资源.2.无 ...
- Android系统进程间通信Binder机制在应用程序框架层的Java接口源代码分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6642463 在前面几篇文章中,我们详细介绍了A ...
- Linux下Samba的配置
前言: 为了实现windows 和 Linux以及其它操作系统之间的资源共享,软件商推出nfs 和samba两种解决方案.因为市场上缺乏象pc-nfs那样的client工具,使得Linux和windo ...
- VS2008找不到MFC90d.dll错误解决方法
问题是在更新嵌入的清单文件时发生的,由于FAT32的原因而未能更新嵌入的清单文件,于是我们有如下两种解决方法: (1)不启用增量链接.在项目的“属性|配置属性|链接器|常规”中的“启用增量链接”选择“ ...
- ThinkPHP实现跨模块调用操作方法概述
ThinkPHP实现跨模块调用操作方法概述 投稿:shichen2014 字体:[增加 减小] 类型:转载 使用 $this 可以调用当前模块内的方法,但是很多情况下经常会在当前模块中调用其他模块 ...
- 在win2008 r2主域控制域上打开“组策略管理”报错“未打开组策略对对象。你可能没有合适的权限”
在win2008 r2主域控制域上打开“组策略管理”报错“未打开组策略对对象.你可能没有合适的权限” 打开组策略管理其它选项提示:找不到指定路径.之前做过的操作:取消域控主机上的共享目录sysvol和 ...