Python 3
In Python 3, this question doesn't apply. The plain int type is unbounded.
However, you might actually be looking for information about the current interpreter's word size, which will be the same as the machine's word size in most cases. That information is still available in Python 3 as sys.maxsize, which is the maximum value representable by a signed word. Equivalently, it's the size of the largest possible list or in-memory sequence.
Generally, the maximum value representable by an unsigned word will be sys.maxsize * 2 + 1, and the number of bits in a word will be math.log2(sys.maxsize * 2 + 2). See this answer for more information.
Python 2
In Python 2, the maximum value for plain int values is available as sys.maxint:
>>> sys.maxint # on my system, 2**63-1
9223372036854775807
You can calculate the minimum value with -sys.maxint - 1 as shown in the docs.
Python seamlessly switches from plain to long integers once you exceed this value. So most of the time, you won't need to know it.
Answer from senderle on Stack OverflowPython 3
In Python 3, this question doesn't apply. The plain int type is unbounded.
However, you might actually be looking for information about the current interpreter's word size, which will be the same as the machine's word size in most cases. That information is still available in Python 3 as sys.maxsize, which is the maximum value representable by a signed word. Equivalently, it's the size of the largest possible list or in-memory sequence.
Generally, the maximum value representable by an unsigned word will be sys.maxsize * 2 + 1, and the number of bits in a word will be math.log2(sys.maxsize * 2 + 2). See this answer for more information.
Python 2
In Python 2, the maximum value for plain int values is available as sys.maxint:
>>> sys.maxint # on my system, 2**63-1
9223372036854775807
You can calculate the minimum value with -sys.maxint - 1 as shown in the docs.
Python seamlessly switches from plain to long integers once you exceed this value. So most of the time, you won't need to know it.
If you just need a number that's bigger than all others, you can use
float('inf')
in similar fashion, a number smaller than all others:
float('-inf')
This works in both python 2 and 3.
How do I represent Integer.MAX_VALUE (Java) in Python?
Does Python have something similar to MAX_INT / std::numeric_limits<T>::max()} as in C++?
Using iterators to find the index of the min or max value in a list
Why is random.randint(0,8) inclusive of the max, but range(0,8) is exclusive?
Videos
It seems there is no equivalent of Integer.MAX_VALUE in Python, since the integer type is unbounded. The reason I'm looking at a way to represent it is because I found Java code that I want to translate and run in Python. Both programs are attempted solutions to this stock trading problem.
Java code (Line 3):
public class Solution {
public int maxProfit(int prices[]) {
int minprice = Integer.MAX_VALUE;
int maxprofit = 0;
for (int i = 0; i < prices.length; i++) {
if (prices[i] < minprice)
minprice = prices[i];
else if (prices[i] - minprice > maxprofit)
maxprofit = prices[i] - minprice;
}
return maxprofit;
}
}
I was able to come up with the code below, which runs but outputs the wrong result. I'm looking for a way to assign the right value to minprice which would lead to the desired output - I put 0 but it's just a filler.
Python code (Line 3):
class Solution:
def maxProfit(self, prices: List[int]) -> int:
minprice=0
maxprofit=0
l=len(prices)
for i in range(l):
if prices[i]<minprice:
minprice=prices[i]
elif prices[i]-minprice>maxprofit:
maxprofit=prices[i]-minprice
return maxprofit My question is:
-
In the original Java program, how does the
Integer.MAX_VALUEwork? How does it help with the use ofminpricelater? -
How can I replace it sensibly in the Python program?