11

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

My understanding:

  1. long() will always return a long
  2. int() will return an int or a long (if its too big)
  3. so int() is sufficient to dynamically get a int/long based on its value

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?


Documentation refered:

class 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.

class long(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.


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)
Srinath Ganesh
  • 2,496
  • 2
  • 30
  • 60
  • 1
    `long` forces the result to a long int (extended precision), regardless of the magnitude. So `int(1)` and `long(1)` aren't the same. In Python 3, the distinction is more internal, but in Python 2, the difference is visible when you display the numbers. – Tom Karzes Nov 12 '17 at 11:47
  • interesting to note that `int` values are automatically changed to `long` on overflow... https://pastebin.com/5PeprCEi – Attie Nov 12 '17 at 11:52
  • 1
    See https://www.python.org/dev/peps/pep-0237/ for some history (though note that what was proposed there doesn't quite match what was actually implemented). The choice mattered once, but there's little reason to care about the difference between `int` versus `long` since Python 2.3. – Mark Dickinson Nov 12 '17 at 12:20
  • 1
    @TomKarzes: In Python 3 there isn't really a distinction at all, internal or otherwise. There's no optimised "short" int type hidden in the Python 3 int implementation: everything is a "long" int. – Mark Dickinson Nov 12 '17 at 12:31
  • So there is no more *long* in 3+ version? – Srinath Ganesh Nov 12 '17 at 15:19
  • Check this link https://stackoverflow.com/questions/2104884/how-does-python-manage-int-and-long/53338502#53338502 You will get a better understanding – Nader Belal Nov 16 '18 at 14:53

1 Answers1

14

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

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

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

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
AlphaBet
  • 156
  • 1
  • 4