python2.x 和 python3.x的区别汇总

来源:互联网 发布:苹果5刷机软件 编辑:程序博客网 时间:2024/06/14 13:25

1、编码方面:

python3.x源码文件默认使用utf-8编码,所以以下代码是合法的: 
      >>>中国 = “data”

      >>>print  (中国)

data


2、语法:

(1)关于模块urllib

       在python2.x里,urllib可以直接import urllib 使用,eg: data = urllib.urlopen(url)

       但是在python3.x里,urllib应该import urllib.request使用,eg: data = urllib.request.urlopen(url)

(2)输入函数:

python2.x中输入函数是raw_input ,而python3.x中输入函数是input

5、数据类型 :

(1)python3.x去除了long类型,现在只有一种整型——int,但是它的行为就像python2.x中的long 
(2)新增了bytes类型,对应于2.X版本的八位串,定义一个bytes字面量的方法如下: 
       >>> b = b'china' 
       >>> type(b) 
        <type 'bytes'> 
str对象和bytes对象可以使用.encode() (str -> bytes) or .decode() (bytes -> str)方法相互转化。 
        >>> s = b.decode() 
        >>> s 
         'china' 
        >>> b1 = s.encode() 
         >>> b1 
         b'china' 

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




原创粉丝点击