一般除法

python默认的除法是整除:

print(1/3)

输出:0

若想实现普通除法,可以有以下方法:

1、转为实数运算:除数或被除数至少一个浮点型;

print(1/3.0)

print(1.0/3)

输出都是:0.333333333333

2、改变除法的方式,加入语句:from __future__ import division

3、命令行操作:启用命令行开关,-Qnew

Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。

C:\Users\#####>python -Qnew
Python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit (AMD64)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> 1/3
0.3333333333333333
>>>

 

整除

1、可以使用一般除法,不作调整

2、强制整除:“//”:

print(1//3)

输出为:0

 

 

posted on 2012-10-22 10:51  森林之子  阅读(738)  评论(0编辑  收藏  举报