Is there no limit to the minimum integer value of Python?

Asked 2 years ago, Updated 2 years ago, 93 views

In Java, Integer.MIN_VALUE or Integer.MAX_VALUE

In C++, <climits> obtained an integer minimum/maximum value.

I wonder if there is a minimum and maximum value in Python.

python integer

2022-09-21 15:26

1 Answers

import sys

t1 = sys.maxint
t2 = sys.maxint+1 #Automatically transforms to long if it exceeds the int range

print(t1)
print(t2)
print(type(t1))
print(type(t2))

Output:

9223372036854775807
9223372036854775808
<type 'int'>
<type 'long'>

Formally, -sys.maxint-1 is the way to obtain the minimum value. link

import sys

t1 = sys.maxsize
t2 = sys.maxsize+1 #This is also type int

print(t1)
print(t2)
print(type(t1))
print(type(t2))

Output:

9223372036854775807
9223372036854775808
<class 'int'>
<class 'int'>

Python 3 treats integers beyond the int range as type int.


2022-09-21 15:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.