oracle12.1和12.2区别_Python知识:Python 3.x和2.x版本的使用区别

使用Python时都需要安装相应的版本,不同的版本适用性也不一样。

今天从除法算子、打印功能、Unicode、Xrange、错误处理、未来模块方面看看Python2.x和Python3.x之间的区别。

7539d43c26b4b8bdd2cf68fbc5c30255.png

除法算子

在移植代码或在python2.x中执行python3.x代码时,要注意整数除法的更改:最好使用浮动值(如7.0/5或7/5.0)来获得预期的结果。

print 7 / 5 
  
print -7 / 5     
  
    
  
''' 
  
Output in Python 2.x 
  
1 
  
-2 
  
Output in Python 3.x : 
  
1.4 
  
-1.4

打印功能

print关键字在Python2.x中被打印()函数在Python3.x中。

如果在Python 2之后添加了空格,解释器将其计算为表达式,则括号在Python 2中起作用。

注意:如果在python 3.x中不使用括号,我们就会得到SyntaxError。

print 'Hello, Geeks'      # Python 3.x doesn't support 
  
print('Hope You like these facts') 
  
    
  
''' 
  
Output in Python 2.x : 
  
Hello, Geeks 
  
Hope You like these facts 
  
    
  
Output in Python 3.x : 
  
File "a.py", line 1 
  
    print 'Hello, Geeks' 
  
                       ^ 
  
SyntaxError: invalid syntax 

Unicode:

在Python 2中,隐式str类型是ASCII。

在Python3.x中,隐式str类型是Unicode。

print(type('default string ')) 
  
print(type(b'string with b ')) 
  
    
  
''' 
  
Output in Python 2.x (Bytes is same as str) 
  
<type 'str'> 
  
<type 'str'> 
  
    
  
Output in Python 3.x (Bytes and str are different) 
  
<class 'str'> 
  
<class 'bytes'> 
  
''' 

Python2.x也支持Unicode

print(type('default string ')) 
  
print(type(u'string with b ')) 
  
    
  
''' 
  
Output in Python 2.x (Unicode and str are different) 
  
<type 'str'> 
  
<type 'unicode'> 
  
    
  
Output in Python 3.x (Unicode and str are same) 
  
<class 'str'> 
  
<class 'str'> 
  
  ''' 

Xrange:

Python2.x的xrange()在Python3.x中不存在。

在Python2.x中,Range返回一个列表,即range(3)返回[0,1,2],

而xrange返回xrange对象,即xrange(3)返回与Java迭代器类似的迭代器对象,并在需要时生成数字。

range()需要多次迭代相同的序列,提供了一个静态列表。

Xrange()需要每次都重新构造序列。Xrange()不支持片和其他列表方法。

Xrange()的优点:当任务在一个大范围内迭代时,节省内存。

在Python3.x中,Range函数执行Python2.x中的xrange函数,坚持使用Range保持代码的可移植性

for x in xrange(1, 5): 

    print(x), 
  
    
  
for x in range(1, 5): 
  
    print(x), 
  
    
  
''' 
  
Output in Python 2.x 
  
1 2 3 4 1 2 3 4 
  
    
  
Output in Python 3.x 
  
NameError: name 'xrange' is not defined 

错误处理:

在python 3.x中,必须要使用“as”关键字。

try: 
  
    trying_to_check_error 
  
except NameError, err: 
  
    print err, 'Error Caused'   # Would not work in Python 3.x 
  
    
  
''' 
  
Output in Python 2.x: 
  
name 'trying_to_check_error' is not defined Error Caused 
  
    
  
Output in Python 3.x : 
  
File "a.py", line 3 
  
    except NameError, err: 
  
                    ^ 
  
SyntaxError: invalid syntax 
  
''' 
try: 
  
     trying_to_check_error 
  
except NameError as err: # 'as' is needed in Python 3.x 
  
     print (err, 'Error Caused') 
  
    
  
''' 
  
Output in Python 2.x: 
  
(NameError("name 'trying_to_check_error' is not defined",), 'Error Caused') 
  
    
  
Output in Python 3.x : 
  
name 'trying_to_check_error' is not defined Error Caused 
 
''' 

_模块:

__WORWORY__模块帮助迁移到Python3.x。

如果想在2.x代码中支持Python3.x,使用__future__在我们的代码中导入。

例如,在下面的Python2.x代码中,我们使用Python3.x的整数除法行为。

# In below python 2.x code, division works 
  
# same as Python 3.x because we use  __future__ 
  
from __future__ import division 
  
    
  
print 7 / 5 
  
print -7 / 5 

产出:

1.4 

-1.4 

在Python2.x中使用方括号的另一个例子是_WORWORY__模块:

from __future__ import print_function     
  
    
  
print('GeeksforGeeks') 

产出:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值