Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python3不会像python2那样自动截断_Python_Python 3.x_Casting_Integer_Truncate - Fatal编程技术网

Python3不会像python2那样自动截断

Python3不会像python2那样自动截断,python,python-3.x,casting,integer,truncate,Python,Python 3.x,Casting,Integer,Truncate,在python2中,如果你做了 n = 12 n /= 10 n将变成1 在python 2中, 上述情况将导致n为1.2,即使它是在一个整数参数中传递的,如 def foo(self, n: int) -> bool: print (n / 10) return True 简单的解决方法是将其转换为如下所示的整数: n = int(n/10) 但这相当耗费内存/时间。python3中有更好的替代方法吗?//是整数除法,因此它会从结果中删除小数。使用/

在python2中,如果你做了

n = 12
n /= 10
n将变成1

在python 2中, 上述情况将导致n为1.2,即使它是在一个整数参数中传递的,如

def foo(self, n: int) -> bool:
        print (n / 10)
        return True
简单的解决方法是将其转换为如下所示的整数:

n = int(n/10)

但这相当耗费内存/时间。python3中有更好的替代方法吗?

//是整数除法,因此它会从结果中删除小数。

使用
/
运算符可以达到与python2相同的效果n=n//10或
n//=10
是浮点除法(称为“真除法”),并且
//
强制整数除法尝试使用
12//10