Python: 探究py2与py3除法的区别
2017-08-15 10:16
309 查看
起因
在用python2解释器运行python3代码的时候,出现了bug。debug后发现是因为python3中的/ 原本表示 精确除法,却被python2解释器解释成了 地板除,最终导致了错误。因此我上网查阅了相关资料,并总结如下表:总结
version | / | // |
---|---|---|
py2 | 整数除法时为地板除,浮点数除法时为精确除 | 地板除 |
py3 | 精确除法 | 地板除 |
Test
x = y = 10 x /= 2 # 精确除 y //= 2 # 地板除 print(x, type(x)) # 5.0 <class 'float'> print(y, type(y)) # 5 <class 'int'>
user@user:~$ python Python 2.7.13 |Anaconda 2.4.1 (64-bit)| (default, Dec 20 2016, 23:09:15) [GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2 Type "help", "copyright", "credits" or "license" for more information. Anaconda is brought to you by Continuum Analytics. Please check out: http://continuum.io/thanks and https://anaconda.org >>> 9/2 4 >>> 9.0/2 4.5 >>> 9//2 4 >>> 9.0//2 4.0 >>> float(9)/2 4.5 >>> from __future__ import division >>> 9/2 4.5 >>> >>> [3]+ Stopped python >>> >>> user@user:~$ python3 Python 3.4.3 (default, Nov 17 2016, 01:08:31) [GCC 4.8.4] on linux Type "help", "copyright", "credits" or "license" for more information. >>> 9/2 4.5 >>> 9//2 4 >>> 9.0//2 4.0 >>>
相关文章推荐
- python_py2和py3读写文本区别
- py2和py3的区别
- python计算之/和//除法的区别
- py2和py3的区别
- python 除法 /与//在2.7*和3.*版本的区别
- python2.x与3.x除法的区别
- 从C++和Python除法的区别谈谈求模(Modulus)和取余(Remainder)
- ubuntu下,py2,py3共存,/usr/bin/python: No module named virtualenvwrapper错误解决方法
- Python基础:Python2与Python3除法的区别
- python中除法 “/” 和 "//"的区别
- input函数在py2和py3的区别
- python2.x与3.x除法的区别
- #!/usr/bin/env python与#!/usr/bin/python的区别
- python2和python3的区别
- <Python数据分析>pandas, DataFrame, Index的方法delete和drop的区别
- python中 is 和 == 的区别
- python数据结构及区别
- Python针对特定服务定制的代理工具V2.1-------(简述与2.0的区别之处)
- python2和python3区别
- python爬虫--urllib2和urllib区别