int()和long()之间的区别

2024-05-12 01:12:29 发布

您现在位置:Python中文网/ 问答频道 /正文

python中int(x)long(x)之间的区别是什么

我的理解:

  1. long()将始终返回long
  2. int()将返回一个int或long(如果它太大)
  3. 因此int()足以根据其值动态获取int/long

所以除非上面(1)(2)(3)是不正确的,否则为什么需要long()?当int()完成任务时? 对所有数字范围跳过long()会伤害我吗?


参考文档:

int类(x=0)

Return an integer object constructed from a number or string x, or return 0 if no arguments are given. If x is a number, it can be a plain integer, a long integer, or a floating point number. If x is floating point, the conversion truncates towards zero. If the argument is outside the integer range, the function returns a long object instead.

长类(x=0)

Return a long integer object constructed from a string or number x. If the argument is a string, it must contain a possibly signed number of arbitrary size, possibly embedded in whitespace. The base argument is interpreted in the same way as for int(), and may only be given when x is a string. Otherwise, the argument may be a plain or long integer or a floating point number, and a long integer with the same value is returned. Conversion of floating point numbers to integers truncates (towards zero). If no arguments are given, returns 0L.


代码试验

number = int(number_string) # cast it to integer
print number, "\t", type(number)

number = long(number_string) # cast it to long
print number, "\t", type(number)

Tags: orthenumberstringifobjectisit
1条回答
网友
1楼 · 发布于 2024-05-12 01:12:29

int:整数;相当于Python 2.x中的C long,Python 3.x中的非限制长度

long:长度不受限制的长整数;仅存在于Python 2.x中

因此,在python 3x和about中,您可以使用int()而无需使用long()

希望这能消除你的疑虑?

相关问题 更多 >