int() 和 long() 的区别 [英] Difference between int() and long()

查看:51
本文介绍了int() 和 long() 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int(x) 和有什么区别long(x) 在 python 中

What is the difference between int(x) and long(x) in python

我的理解:

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

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

So unless above (1) (2) (3) are incorrect, why do you need long()? when int() gets the job done? skipping long() for all number ranges will hurt me?

参考文档:

class int(x=0)

返回由数字或字符串 x 构造的整数对象,或如果没有给出参数,则返回 0.如果 x 是一个数字,它可以是一个普通整数、长整数或浮点数.如果 x 是浮点数,转换向零截断.如果论证超出整数范围,函数返回一个长对象相反.

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.

class long(x=0)

返回一个由字符串或数字 x 构造的长整型对象.如果参数是一个字符串,它必须包含一个可能有符号的数字任意大小,可能嵌入在空格中.基本论点是以与 int() 相同的方式解释,并且只能在 x 时给出是一个字符串.否则,参数可能是一个普通或长整数或一个浮点数,和一个长整数相同的值是回.将浮点数转换为整数会截断(趋向于零).如果没有给出参数,则返回 0L.

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.

<小时>

代码实验


code experimented

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)

推荐答案

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

int: Integers; equivalent to C longs in Python 2.x, non-limited length in Python 3.x

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

long: Long integers of non-limited length; exists only in Python 2.x

因此,在 python 3.x 及更高版本中,您可以使用 int() 而不是 long().

So, in python 3.x and above, you can use int() instead of long().

希望这能消除您的疑虑吗?

Hope this clears your doubt?

这篇关于int() 和 long() 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆