Python2和Python3的区别?

最大的不同 名字不同!!!

1.python2 ascii python3 utf-8 内存形式unicode

py2 的默认编码是ASCII,py3的默认编码是UTF-8。
python2 不在开头声明解码格式时,运行会报错

SyntaxError: Non-ASCII character '\xe7' in file /home/jacky/Desktop/test_for_try_python.py on line 24, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

解决方法如下:

在代码的开头添加utf-8. Python解释器会默认使用utf-8去解析

#coding=utf-8
#或者
#-*- coding:utf-8 -*-
#左右两边不能有空格

2.print 函数

#python2
print 'a'
print name_a # name_a = 'LJL'
print 100

#python3
print("a")
print(name_a) # name_a = "LJL"
print(100)

3. py3 input py2 rawinput

Python2

raw_input( )函数 无论用户输入的是什么类型的值,都将其视为字符类型
input( )函数用户必须明确自己想要输入的是什么类型的值,数字格式的值可以直接输入,函数接收到的也将会是一 个数字格式的值,如果想要输入字符格式的值,那么必须在输入时必须加上‘’或“”来告诉input函数用户希望输入的是字符格式的值

Python3

input 同py2的rawinput 总是字符类型

总结:python3的input 相当于 python2的rawinput

4.range xrange

首先,只有在python2才有xrange和range,python3只有range

Python2

range()

在py2中,range返回一个列表


x = range(0, 6)
print(type(x))  # <type 'list'>
print(x) # [0,1,2,3,4,5]
xrange()

xrange得到的是一个生成器对象

x = xrange(0,6)
print(type(x))  # <type 'xrange'>
print(x) # xrange(6)
for i in xrange(0,6):
	print(i)
# 0
# 1
# 2
# 3
# 4
# 5

python3 range() 同py2的 xrange()

5. int long

Python3:Python3中int类型的范围是动态长度的,正整数或者负整数,用sys.getsizeof()可以看int占了几位.

Python2:Python2中long类型的范围是无限大小.

6./ // 除法 地板除法

三种除法:

  1. 传统的除法,我称之为整型地板除。在C、C++、Java中常见,特点是整数相除舍弃小数取整,浮点数相除则保留小数(如果有)。
 >>>1/2
 0
 >>>1.0/2.0
 0.5
  1. 真实的除法,我称之为精确除法。特点是无论整数还是浮点数,均保留小数(如果有)。
>>>1/2
0.5
>>>1.0/2.0
40.5
  1. 只留整数的除法,我称之为完全地板除。特点是无论整数还是浮点数,均不保留小数(如果有)。
>>>1//2
0
>>>1.0//2.0
0.0

python2

  • / 表示整型地板除法
>>> 	1//3 
 0
>>> 1.0 // 2.0
>0.5
  • 精确除法 导入 from future import division
 >>>from __future__ import division 
 >>>1/3 
 0.3333333333333333
  • 完全地板除 //
>>> 1.0 // 2.0
0.0

python3

  • 默认精确除法 /
>>> 1/2
0.5
>>> 1.0/2.0
0.5
  • 完全地板除法 //
>>> 1.0//2.0
0.0
>>> 1//2
0

7.类

创建类时,py2分为经典类和新式类,新式类就是继承object的类,经典类是没有继承的类,而py3中全部是新式类,默认继承object。在属性查找时,经典类查找方式为深度优先,新式类是广度优先。仅py3中有类的mro函数方法,输出继承父类的顺序列表。

重用父类属性的super()使用方法不同,py2中super()需要输入本身类名和自身作为参数,而py3中不用输入参数,可以直接使用替代上一级父类。

8. 异常处理

异常处理
Python 2中捕获异常一般用下面的语法

try:
    1/0 
except ZeroDivisionError, e:
    print str(e)
#或者

try:
    1/0 
except ZeroDivisionError as e:
    print str(e)

Python 3中不再支持前一种语法,必须使用as关键字。

9. map、filter函数

在Python 2中,map函数返回list,而在Python 3中,map函数返回iterator。

#Python 2

map(lambda x: x+1, range(5))
#输出

[1, 2, 3, 4, 5]


#Python 3

map(lambda x: x+1, range(5))
#输出

<map object at 0x7ff5b103d2b0>


list(map(lambda x: x+1, range(5)))
#输出

[1, 2, 3, 4, 5]

filter函数在Python 2和Python 3中也是同样的区别。

10. has_key

Python 3中的字典不再支持has_key方法

Python 2

person = {"age": 30, "name": "Xiao Wang"}
print "person has key \"age\": ", person.has_key("age")
print "person has key \"age\": ", "age" in person
#输出

person has key "age":  True
person has key "age":  True


#Python 3

person = {"age": 30, "name": "Xiao Wang"}
print("person has key \"age\": ", "age" in person)
#输出

person has key "age":  True


print("person has key \"age\": ", person.has_key("age"))
#输出
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'dict' object has no attribute 'has_key'

此外还有模块导入、缩进、for循环等等

这是先写这么多 等待补充和指正!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值