python3.4学习笔记 3.x和2.x的区别,持续更新

作者&投稿:岛点 (若有异议请与网页底部的电邮联系)
Python3.x和Python2.x的区别~

python3.4学习笔记(四) 3.x和2.x的区别

在2.x中:print html,3.x中必须改成:print(html)

import urllib2
ImportError: No module named 'urllib2'
在python3.x里面,用urllib.request代替urllib2

import thread
ImportError: No module named 'thread'
在python3.x里面,用_thread(在前面加一个下划线)代替thread

在2.x中except Exception,e : 3.x中改为except (Exception):

=================================
print函数
虽然print语法是Python 3中一个很小的改动,且应该已经广为人知,但依然值得提一下:Python 2中的print语句被Python 3中的print()函数取代,这意味着在Python 3中必须用括号将需要输出的对象括起来。
在Python 2中使用额外的括号也是可以的。但反过来在Python 3中想以Python2的形式不带括号调用print函数时,会触发SyntaxError。
Python 2.7.6

print 'Python', python_version()
print 'Hello, World!'
print('Hello, World!')
print "text", ; print 'print more text on the same line'
输出:
Hello, World!
Hello, World!
text print more text on the same line
---------------------------

Python 3.4.1
print('Python', python_version())
print('Hello, World!')

print("some text,", end="")
print(' print more text on the same line')
输出:
Hello, World!
some text, print more text on the same line
print 'Hello, World!'
File "", line 1
print 'Hello, World!'
^
SyntaxError: invalid syntax

注意:在Python中,带不带括号输出”Hello World”都很正常。
但如果在圆括号中同时输出多个对象时,就会创建一个元组,这是因为在Python 2中,print是一个语句,而不是函数调用。

print 'Python', python_version()
print('a', 'b')
print 'a', 'b'
Python 2.7.7
('a', 'b')
a b

---------------------------------
整数除法
由于人们常常会忽视Python 3在整数除法上的改动(写错了也不会触发Syntax Error),所以在移植代码或在Python 2中执行Python 3的代码时,需要特别注意这个改动。

所以,我还是会在Python 3的脚本中尝试用float(3)/2或 3/2.0代替3/2,以此来避免代码在Python
2环境下可能导致的错误(或与之相反,在Python 2脚本中用from __future__ import division来使用Python
3的除法)。

Python 2.7.6
3 / 2 = 1
3 // 2 = 1
3 / 2.0 = 1.5
3 // 2.0 = 1.0

Python 3.4.1
3 / 2 = 1.5
3 // 2 = 1
3 / 2.0 = 1.5
3 // 2.0 = 1.0
---------------------------------
Unicode
Python 2有基于ASCII的str()类型,其可通过单独的unicode()函数转成unicode类型,但没有byte类型。
而在Python 3中,终于有了Unicode(utf-8)字符串,以及两个字节类:bytes和bytearrays。

Python 2.7.6
print type(unicode('this is like a python3 str type'))

print type(b'byte type does not exist')

print 'they are really' + b' the same'
they are really the same
print type(bytearray(b'bytearray oddly does exist though'))


Python 3.4.1 has
print('and Python', python_version(), end="")
print(' also has', type(bytearray(b'bytearrays')))
and Python 3.4.1 also has
1
'note that we cannot add a string' + b'bytes for data'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
in ()
----> 1 'note that we cannot add a string' + b'bytes for data'

TypeError: Can't convert 'bytes' object to str implicitly

=================================

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 语句的变化
在2.x中except Exception,e : 3.x中改为except (Exception):

五、打开文件
原: 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("?")

print不再是语句,而是函数,比如原来是 print 'abc' 现在是 print('abc') 但是 python2.6+ 可以使用 from __future__ import print_function 来实现相同功能 .

python3.4学习笔记(四) 3.x和2.x的区别

在2.x中:print html,3.x中必须改成:print(html)

import urllib2
ImportError: No module named 'urllib2'
在python3.x里面,用urllib.request代替urllib2

import thread
ImportError: No module named 'thread'
在python3.x里面,用_thread(在前面加一个下划线)代替thread

在2.x中except Exception,e : 3.x中改为except (Exception):

=================================
print函数
虽然print语法是Python 3中一个很小的改动,且应该已经广为人知,但依然值得提一下:Python 2中的print语句被Python 3中的print()函数取代,这意味着在Python 3中必须用括号将需要输出的对象括起来。
在Python 2中使用额外的括号也是可以的。但反过来在Python 3中想以Python2的形式不带括号调用print函数时,会触发SyntaxError。
Python 2.7.6

print 'Python', python_version()
print 'Hello, World!'
print('Hello, World!')
print "text", ; print 'print more text on the same line'
输出:
Hello, World!
Hello, World!
text print more text on the same line
---------------------------

Python 3.4.1
print('Python', python_version())
print('Hello, World!')

print("some text,", end="")
print(' print more text on the same line')
输出:
Hello, World!
some text, print more text on the same line
print 'Hello, World!'
File "<ipython-input-3-139a7c5835bd>", line 1
print 'Hello, World!'
^
SyntaxError: invalid syntax

注意:在Python中,带不带括号输出”Hello World”都很正常。
但如果在圆括号中同时输出多个对象时,就会创建一个元组,这是因为在Python 2中,print是一个语句,而不是函数调用。

print 'Python', python_version()
print('a', 'b')
print 'a', 'b'
Python 2.7.7
('a', 'b')
a b

---------------------------------
整数除法
由于人们常常会忽视Python 3在整数除法上的改动(写错了也不会触发Syntax Error),所以在移植代码或在Python 2中执行Python 3的代码时,需要特别注意这个改动。

所以,我还是会在Python 3的脚本中尝试用float(3)/2或 3/2.0代替3/2,以此来避免代码在Python
2环境下可能导致的错误(或与之相反,在Python 2脚本中用from __future__ import division来使用Python
3的除法)。

Python 2.7.6
3 / 2 = 1
3 // 2 = 1
3 / 2.0 = 1.5
3 // 2.0 = 1.0

Python 3.4.1
3 / 2 = 1.5
3 // 2 = 1
3 / 2.0 = 1.5
3 // 2.0 = 1.0
---------------------------------
Unicode
Python 2有基于ASCII的str()类型,其可通过单独的unicode()函数转成unicode类型,但没有byte类型。
而在Python 3中,终于有了Unicode(utf-8)字符串,以及两个字节类:bytes和bytearrays。

Python 2.7.6
print type(unicode('this is like a python3 str type'))
<type 'unicode'>
print type(b'byte type does not exist')
<type 'str'>
print 'they are really' + b' the same'
they are really the same
print type(bytearray(b'bytearray oddly does exist though'))
<type 'bytearray'>

Python 3.4.1 has <class 'bytes'>
print('and Python', python_version(), end="")
print(' also has', type(bytearray(b'bytearrays')))
and Python 3.4.1 also has <class 'bytearray'>
1
'note that we cannot add a string' + b'bytes for data'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-13-d3e8942ccf81> in <module>()
----> 1 'note that we cannot add a string' + b'bytes for data'

TypeError: Can't convert 'bytes' object to str implicitly

=================================

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 语句的变化
在2.x中except Exception,e : 3.x中改为except (Exception):

五、打开文件
原: 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("?")


求助ython2.7加载pygame总是失败,折腾了我一天了
具体步骤如下:到下载pygame的安装包,注意选择合适的版本,比如适合Python3.4的版本文件名中包含cp34,适合64位操作系统的版本,文件名中包含amd64;选择开始>运行,输入cmd打开命令提示符,在命令提示符中输入:pipinstall下载pygame安装包的完整路径打开Python,输入importpygame,如果没有报错则说明安装成...

江山市18452349902: python3.4学习笔记 3.x和2.x的区别,持续更新 -
繁穆寒痛: python3.4学习笔记(四) 3.x和2.x的区别 在2.x中:print html,3.x中必须改成:print(html) import urllib2 ImportError: No module named 'urllib2' 在python3.x里面,用urllib.request代替urllib2 import thread ImportError: No module named 'thread' 在...

江山市18452349902: 刚学的python,版本是3.4的,想买本书看看,但是网上都是介绍2.x的,所以求本介绍3.x的 -
繁穆寒痛: learn python 中文版叫《python学习手册》是介绍python3的.

江山市18452349902: Pycharm怎么打包Python脚本 -
繁穆寒痛: python3是必须的,pyqt是做界面的,pycharm是优秀的IDE,pyinstaller生成可执行文件,.1.先按照教程安装python3,QT,pycharm软件,因为目前PyQt5只支持python3.4,所以安装python3.4.参考教程PyQt5+python3+pycharm开发环境配置 在环...

江山市18452349902: 如何使用Python3.4与Linux shell交互 -
繁穆寒痛: 下面来看下在python3.4中,如何与Linux交互的.在python里面可以操作linux的命令有1,os.system("cmd")2,os.open("cmd")3,spawn*4,subprocess 在最新的python里,推荐使用subprocess来与shell通信,它会生成一个子进程来连接输出,输...

江山市18452349902: 现在想学Python,用哪个版本合适,官网上是3.4和2.7.10 新手入门资料要有 -
繁穆寒痛: 建议学习Python 3.4.Python 2与Python 3是有差别的,并不完全兼容,这样,在开发应用的时候,会有一些问题,主要是有一些Python 2的包可能无法在Python 3下使用,尤其是一些数学计算、科学计算的包.但是Python 3从语言方面还有更先...

江山市18452349902: 如何从Python 3.4 升级到 Python 3.5 -
繁穆寒痛: sudo add-apt-repository ppa:fkrull/deadsnakes sudo apt-get update sudo apt-get install python3.5 启动时要输入python3.5.需要注意:升级到ubuntu15.10.ubuntu15.10默认的python3是python3.4,不过你可以通过命令:sudo apt-get install python3.5 来安装python3.5.不过,为了安全起见,请使用虚拟环境

江山市18452349902: 关于Python 的经典入门书籍有哪些 -
繁穆寒痛: 《Python学习笔记--皮大庆》 《笨办法学Python》 《简明Python教程》 《Python编程入门经典》 《python基础教程》 ----------------------------------------------------------- 以上是我初学Python时看的入门书籍,希望能帮到你.

江山市18452349902: 怎样用python3.4编程判断函数,例如奇偶函数的判断,主要是自己能够任意输入函数,让pytho -
繁穆寒痛: i = input('Input number: ') if int(i) % 2 == 1:print('奇数') else:print('偶数')

江山市18452349902: 笨办法学python 34课怎么写 -
繁穆寒痛: 本书是一本Python入门书籍,适合对计算机了解不多,没有学过编程,但对编程感兴趣的读者学习使用.这本书以习题的方式引导读者一步一步学习编程,从简单的打印一直讲到完整项目的实现,让初学者从基础的编程技术入手,最终体验到软件开发的基本过程.本 书结构非常简单,共包括52个习题,其中26个覆盖了输入/输出、变量和函数三个主题,另外26个覆盖了一些比较高级的话题,如条件判断、循环、类和对 象、代码测试及项目的实现等.每一章的格式基本相同,以代码习题开始,按照说明编写代码,运行并检查结果,然后再做附加练习.

江山市18452349902: python3.4中if语句用法 -
繁穆寒痛: guess=int(input("Enter an integer")) 这一句最后是两个右括号,分别与input和int函数对应,你只写了一个 最后 一句 print('Done') 加上引号,Done不是内部变量

你可能想看的相关专题

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网