Maximum and Minimum values for ints

前端 未结 9 1907
北荒
北荒 2020-11-22 06:36

I am looking for minimum and maximum values for integers in python. For eg., in Java, we have Integer.MIN_VALUE and Integer.MAX_VALUE. Is there som

9条回答
  •  臣服心动
    2020-11-22 07:14

    In Python integers will automatically switch from a fixed-size int representation into a variable width long representation once you pass the value sys.maxint, which is either 231 - 1 or 263 - 1 depending on your platform. Notice the L that gets appended here:

    >>> 9223372036854775807
    9223372036854775807
    >>> 9223372036854775808
    9223372036854775808L
    

    From the Python manual:

    Numbers are created by numeric literals or as the result of built-in functions and operators. Unadorned integer literals (including binary, hex, and octal numbers) yield plain integers unless the value they denote is too large to be represented as a plain integer, in which case they yield a long integer. Integer literals with an 'L' or 'l' suffix yield long integers ('L' is preferred because 1l looks too much like eleven!).

    Python tries very hard to pretend its integers are mathematical integers and are unbounded. It can, for instance, calculate a googol with ease:

    >>> 10**100
    10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000L
    

提交回复
热议问题