print函数:(Python3中print为一个函数,必须用括号括起来;Python2中print为class)

Python 2 的 print 声明已经被 print() 函数取代了,这意味着我们必须包装我们想打印在小括号中的对象。

Python 2

1
2
3
4
print 'Python', python_version()
print 'Hello, World!'
print('Hello, World!')
print "text", ; print 'print more text on the same line'

run result:
Python 2.7.6
Hello, World!
Hello, World!
text print more text on the same line

Python 3

1
2
3
4
print('Python', python_version())
print('Hello, World!')
print("some text,", end="")
print(' print more text on the same line')

run result:
Python 3.4.1
Hello, World!
some text, print more text on the same line


通过input()解析用户的输入:(Python3中input得到的为str;Python2的input的到的为int型,Python2的raw_input得到的为str类型)统一一下:Python3中用input,Python2中用row_input,都输入为str

幸运的是,在 Python 3 中已经解决了把用户的输入存储为一个 str 对象的问题。为了避免在 Python 2 中的读取非字符串类型的危险行为,我们不得不使用 raw_input() 代替。

Python 2
Python 2.7.6
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type “help”, “copyright”, “credits” or “license” for more information.

>>> my_input = input('enter a number: ')

enter a number: 123

>>> type(my_input)
<type 'int'> >>> my_input = raw_input('enter a number: ') enter a number: 123 >>> type(my_input)
<type 'str'>

Python 3
Python 3.4.1
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type “help”, “copyright”, “credits” or “license” for more information.

>>> my_input = input('enter a number: ')

enter a number: 123

>>> type(my_input)
<class 'str'>

整除:(没有太大影响)(Python3中/表示真除,%表示取余,//表示地板除(结果取整);Python2中/表示根据除数被除数小数点位得到结果,//同样表示地板除)统一一下:Python3中/表示真除,%表示取余,//结果取整;Python2中带上小数点/表示真除,%表示取余,//结果取整

Python 2

1
2
3
4
5
print 'Python', python_version()
print '3 / 2 =', 3 / 2
print '3 // 2 =', 3 // 2
print '3 / 2.0 =', 3 / 2.0
print '3 // 2.0 =', 3 // 2.0

run result:
Python 2.7.6
3 / 2 = 1
3 // 2 = 1
3 / 2.0 = 1.5
3 // 2.0 = 1.0

Python 3

1
2
3
4
5
print('Python', python_version())
print('3 / 2 =', 3 / 2)
print('3 // 2 =', 3 // 2)
print('3 / 2.0 =', 3 / 2.0)
print('3 // 2.0 =', 3 // 2.0)

run result:
Python 3.4.1
3 / 2 = 1.5
3 // 2 = 1
3 / 2.0 = 1.5
3 // 2.0 = 1.0


xrange模块:

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

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


待补充。。。

http://chenqx.github.io/2014/11/10/Key-differences-between-Python-2-7-x-and-Python-3-x/

http://www.cnblogs.com/codingmylife/archive/2010/06/06/1752807.html



python 2.4 与 python 3.0 的比较

一、 print 从语句变为函数

原:     print   1, 2+3

改为: print ( 1, 2+3 )

二、range 与 xrange

原 : range( 0, 4 )   结果 是 列表 [0,1,2,3 ]

改为:list( range(0,4) )

原 : xrange( 0, 4 )    适用于 for 循环的变量控制

改为:range(0,4)

三、字符串

原: 字符串以 8-bit 字符串存储

改为: 字符串以 16-bit Unicode 字符串存储

四、try except 语句的变化

原: try:

          ......

     except    Exception, e :

         ......

改为

try:

          ......

     except    Exception as e :

         ......

五、打开文件

原: file( ..... )

    或 open(.....)

改为:

只能用 open(.....)

六、从键盘录入一个字符串

原: raw_input( "提示信息" )

改为: input( "提示信息" )

七、bytes 数据类型

A bytes object is an immutable array. The items are 8-bit bytes, represented by integers in the range 0 <= x < 256.

bytes 可以看成是“字节数组”对象,每个元素是 8-bit 的字节,取值范围 0~255。

由于在 python 3.0中字符串以 unicode 编码存储,当写入二进制文件时,字符串无法直接写入(或读取),必须以某种方式的编码为字节序列后,方可写入。

(一)字符串编码(encode) 为 bytes

例:   s = "张三abc12"

       b = s.encode( 编码方式)

# b 就是 bytes 类型的数据

# 常用的编码方式为 : "uft-16"    , "utf-8", "gbk", "gb2312", "ascii" , "latin1" 等

# 注 : 当字符串不能编码为指定的“编码方式”时,会引发异常

(二) bytes 解码(decode)为字符串

s = "张三abc12"

       b = s.encode( "gbk")    # 字符串 s 编码为 gbk 格式的字节序列

 s1 = b.decode("gbk")   # 将字节序列 b以gbk格式 解码为字符串

# 说明,当字节序列不能以指定的编码格式解码时会引发异常

(三)使用方法举例

#coding=gbk

f = open("c:\\1234.txt", "wb")
s = "张三李四abcd1234"
# -------------------------------
# 在 python2.4 中我们可以这样写:
# f.write( s )
# 但在 python 3.0中会引发异常
# -------------------------------
b = s.encode("gbk")
f.write( b )
f.close()

input("?")

读取该文件的例子:

#coding=gbk

f = open("c:\\1234.txt", "rb")
f.seek(0,2) #定位至文件尾
n = f.tell() #读取文件的字节数
f.seek(0,0) #重新定位至文件开始处
b = f.read( n )
# ------------------------------
# 在 python 2.4 中 b 是字符串类型
# 要 python 3.0 中 b 是 bytes 类型
# 因此需要按指定的编码方式确码
# ------------------------------ 
s = b.decode("gbk")
print ( s )
# ------------------------------
# 在 python 2.4 中 可以写作 print s 或 print ( s )
# 要 python 3.0 中 必须写作 print ( s )
# ------------------------------ 
f.close()
input("?")

运行后应显示:

张三李四abcd1234

(四) bytes序列,一但形成,其内容是不可变的

例:

s="ABCD"

b=s.encode("gbk")

print b[0]       # 显示   65

b[0] = 66   

执行该句,出现异常: 'bytes' object does not support item assignment

八、 chr( K ) 与 ord( c )

python 2.4.2以前

chr( K )   将编码K 转为字符,K的范围是 0 ~ 255

   ord( c )   取单个字符的编码, 返回值的范围: 0 ~ 255

python 3.0

chr( K )   将编码K 转为字符,K的范围是 0 ~ 65535

   ord( c )   取单个字符的编码, 返回值的范围: 0 ~ 65535

九、 除法运算符

python 2.4.2以前

10/3      结果为 3     

python 3.0

10 / 3 结果为 3.3333333333333335

   10 // 3 结果为 3

十、字节数组对象 --- 新增

(一) 初始化

    a = bytearray(   10 )

# a 是一个由十个字节组成的数组,其每个元素是一个字节,类型借用 int

# 此时,每个元素初始值为 0

(二) 字节数组 是可变的

    a = bytearray(   10 )

a[0] = 25

# 可以用赋值语句更改其元素,但所赋的值必须在 0 ~ 255 之间

(三)   字节数组的切片仍是字节数组

(四)   字符串转化为字节数组

#coding=gbk

     s ="你好"

     b = s.encode( "gbk")     # 先将字符串按某种“GBK”编码方式转化为 bytes

     c = bytearray( b )          #再将 bytes 转化为 字节数组

     也可以写作

     c = bytearray( "你好", "gbk")

(五)   字节数组转化为字符串

c = bytearray( 4 )

       c[0] = 65 ; c[1]=66; c[2]= 67; c[3]= 68

      s = c.decode( "gbk" )

       print ( s )

# 应显示: ABCD

(六) 字节数组可用于写入文本文件

#coding=gbk

f = open("c:\\1234.txt", "wb")
s = "张三李四abcd1234"
# -------------------------------
# 在 python2.4 中我们可以这样写:
# f.write( s )
# 但在 python 3.0中会引发异常
# -------------------------------
b = s.encode("gbk")

f.write( b )
c=bytearray( "王五","gbk")
f.write( c )
f.close()

input("?")

RookieDong的补充

1,“import  thread”问题,2.x中的模块thread在3.x中编程"_thread"(需要在前面加一个下划线).否则会出现“ImportError: No module named thread



dd

python3和Python2的区别(被坑太久了)的更多相关文章

  1. python3和python2语法区别

    1.print python2中是print xxx python3中是print(xxx) 2.抛异常except python2中except Exception,e: print "E ...

  2. python3和Python2的区别

    一.print函数 python2 的 print 声明已经被 print() 函数取代了,这意味着我们必须包装我们想打印在小括号中的对象 二.通过input()解析用户的输入 python3中inp ...

  3. Python3.* 和Python2.*的区别

    许多Python初学者都会问:我应该学习哪个版本的Python.对于这个问题,我的回答通常是“先选择一个最适合你的Python教程,教程中使用哪个版本的Python,你就用那个版本.等学得差不多了,再 ...

  4. Python3 和 Python2的区别

    目录 print Python2.7的print不是一个function Python3里的print是一个function. Unicode Python 2 有 ASCII str() 类型,un ...

  5. Python3与Python2的区别汇总

    1.print 在Python3.0  是一个函数,正确输入应该是:print (3x) 2.raw_input 在Python3.0改成input

  6. python3与python2的区别(目前遇到的)

    1.进击的print,变成一个函数,print() 2.urllib大一统,呵呵 3.python3默认绝对路径导入

  7. python3 与 python2的 区别比较

    http://sebug.net/paper/books/dive-into-python3/porting-code-to-python-3-with-2to3.html

  8. python3和python2的区别部分

    字典没有iteritems(),只有items() py2items()返回的是列表,iteritems()返回的是迭代器 py3items()返回的是迭代器

  9. python3与python2的区别 记录一波

    1.性能 Py3.0运行 pystone benchmark的速度比Py2.5慢30%.Guido认为Py3.0有极大的优化空间,在字符串和整形操作上可 以取得很好的优化结果. Py3.1性能比Py2 ...

随机推荐

  1. Detect the Virus - ZOJ 3430(恶心的自动机)

    题目大意:给你一些病毒的特征码,然后再给一些文本,判断每个文本有多少种病毒,不过给的字符串都是加密处理过的,给的每个字符串都有对应一个64以内的一个数(题目里面那个表就是),然后可以把这个64以内的这 ...

  2. .NET中的程序集(Assembly)

    在.NET 中,新引入了一个程序集的概念,就是指经由编译器编译得到的,供CLR进一步编译执行的那个中间产物,在WINDOWS系统中,它一般表现为.dll,或者是.exe的格式,但是要注意,它们跟普通意 ...

  3. 《火球——UML大战需求分析》(第1章 大话UML)——1.2 结构型的UML(Structure Diagram)

    说明: <火球——UML大战需求分析>是我撰写的一本关于需求分析及UML方面的书,我将会在CSDN上为大家分享前面几章的内容,总字数在几万以上,图片有数十张.欢迎你按文章的序号顺序阅读,谢 ...

  4. rabbitmq Clustering Guide--官方

    官方文档地址:http://www.rabbitmq.com/documentation.html A RabbitMQ broker is a logical grouping of one or ...

  5. 解决XCode 4.x SVN无法连接的问题

    XCode升级到4.X版本后,确实好用了不少.但普通都存在SVN无法连接的问题.XCode4.x Source Control功能迁移到了File - Source Control目录下,也出现了一些 ...

  6. [转] 用SBT编译Spark的WordCount程序

    问题导读: 1.什么是sbt? 2.sbt项目环境如何建立? 3.如何使用sbt编译打包scala? [sbt介绍 sbt是一个代码编译工具,是scala界的mvn,可以编译scala,java等,需 ...

  7. iOS应用内支付(内购)的个人开发过程及坑!

    本文会给大家详细介绍iOS内购,这是本人16年5月底的开发过程,希望对看完此篇文章的人有所帮助. 本文基于XcodeVersion 7.3 (7D175)版本,手机是iPhone 6,9.3系统. 部 ...

  8. C#编写QQ找茬外挂

    QQ找茬外挂,用C#代码编写. 使用方法 这个工具的主要运行流程很简单:游戏截图->比较图片->标记图片不同点.实现代码 截图的处理类ScreenCapture: /// /// 提供全屏 ...

  9. Servlet学习--练习示例总结

    醉了醉了..本来想测试下Servlet生命周期的,然后调了好久的错误,还是没成功,不知道为什么不能这样做 贴上代码: import java.io.IOException; import java.i ...

  10. Linq101-Projection

    using System; using System.Linq; namespace Linq101 { class Projection { /// <summary> /// This ...