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码. ...
随机推荐
- Merlin 的魔力: SpringLayout 管理器
摘自http://tech.it168.com/a2009/0211/265/000000265087_all.shtml 摘自http://cache.baiducontent.com/c?m=9f ...
- c#调用Excel绘制图表
c#调用Excel需要引用命名空间 using Microsoft.Office.Interop.Excel; 由于该程序不复杂,主要是根据不同数据画表和图,画的图像也并不复杂,因为画图和画表的操作会 ...
- [CSS3] CSS Display Property: Block, Inline-Block, and Inline
Understanding the most common CSS display types of block, inline-block, and inline will allow you to ...
- java判断数据类型两种方式
instanceof String s = ""; System.out.println(s instanceof String); // true simp ...
- bad interpreter: No such file or directory
经常会遇到这种情况,在windows下写的脚本,代码会在linux下无法执行,错误就是: bad interpreter: No such file or directory 1.原因 这通常都是由于 ...
- 前端--关于css选择器
css选择器就好像表达式一样,返回一组或者一个html元素,后面的样式声明块应用到返回的元素上.所以可以把css选择器理解为某个或者某一类html元素的抽象的写法. 在讲具体的各种选择器之前要提一下选 ...
- PHP学习笔记二十七【重写】
<?php // class Animal{ public $name; protected $price; function cry(){ echo "动物在叫....<br/ ...
- PHP学习笔记十七【面向对象定义类】
<?php class Person{ public $name; public $age; public function speak(){ echo "Hello world&qu ...
- C#总结(2)
有输出,当然有输入.这样才会有人机交互. using System; using System.Collections.Generic; using System.Linq; using System ...
- .Net Memory -- GC基本知识
参考资料: http://blogs.msdn.com/b/tess/archive/2008/04/17/how-does-the-gc-work-and-what-are-the-sizes-of ...