sys.maxint in Python - GeeksforGeeks

01 Oct, 2020
In programming, maxint/INT_MAX denotes the highest value that can be represented by an integer. In some cases, while programming, we may need to assign a value that is larger than any other integer value. Normally one assigns such values manually. For example, consider a list of integers where the minimum value has to be found out using a for loop.
Python
# initializing the listli = [1, -22, 43, 89, 2, 6, 3, 16]  # assigning a larger value manuallycurr_min = 999999  # loop to find minimum valuefor i in range(0, len(li)):      # update curr_min if a value lesser than it is found    if li[i] < curr_min:        curr_min = li[i]  print("The minimum value is " + str(curr_min))
The minimum value is -22

In the approach above we assume that 999999 is the maximum possible value in our list and compare it with other elements to update when a value lesser than it is found. 
This module is used to interact with the interpreter and to access the variables maintained by the interpreter. It can be used to carry out manipulations in the runtime environment. This has to be imported like other packages to utilize the functionalities in it. Python’s sys module provides a variety of functions and constants among which the constant maxint, which can be used to set a positive integer value which is guaranteed to be larger than any other integer. Look at the example below.
Python
# import the moduleimport sys  # initializing the listli = [1, -22, 43, 89, 2, 6, 3, 16]  # assigning a larger value with # maxint constantcurr_min = sys.maxint  # loop to find minimum valuefor i in range(0, len(li)):      # update curr_min if a value lesser     # than it is found    if li[i] < curr_min:        curr_min = li[i]  print("The minimum value is " + str(curr_min))
The minimum value is -22

In the program above, instead of assigning a larger value manually, sys.maxint is used. This constant is supported in Python version 2.x. The value denoted by the constant can be calculated as :
maxint = 231 – 1 (in 32-bit environment)
maxint = 263 – 1 (in 64-bit environment)
In Python 2, adding 1 to the maxint gives the highest possible long int and in Python 2.7, subtracting 1 from maxint gives the smallest possible value for an integer.
Python
# import the moduleimport sys  max_int = sys.maxintmin_int = sys.maxint-1long_int = sys.maxint+1  print("maxint :"+str(max_int)+" - "+str(type(max_int)))print("maxint - 1 :"+str(max_int)+" - "+str(type(min_int)))print("maxint + 1 :"+str(max_int)+" - "+str(type(long_int)))
maxint :9223372036854775807 - <type 'int'>
maxint - 1 :9223372036854775807 - <type 'int'>
maxint + 1 :9223372036854775807 - <type 'long'>

This constant was removed from Python 3, as integers in this version are considered to be of arbitrary length. If you use this constant in Python 3, then you will get the following error. Consider the same example where the minimum value element has to be found out from a list.
Python3
import sys  # initializing the listli = [1, -22, 43, 89, 2, 6, 3, 16]  # assigning a larger value with maxint constantcurr_min = sys.maxint  # loop to find minimum valuefor i in range(0, len(li)):      # update curr_min if a value lesser than it is found    if li[i] < curr_min:        curr_min = li[i]  print("The minimum value is " + str(curr_min))
Output :
AttributeError: module 'sys' has no attribute 'maxint'

This constant was removed as there was no longer a limit for to the value of integers. In Python 3, a constant similar to this was introduced which is sys.maxsize. This returns the highest possible integer value of variable type Py_ssize_t and also, it denotes the pointer size of the platform. This maxsize is considered to limit the size of various data structures like Strings and lists. Another thing to be noted is, in Python 3 the int and long int are merged into one. Look at the example below for better understanding.
Python3
# import the moduleimport sys  # using sys.maxsizemax_int = sys.maxsizemin_int = sys.maxsize-1long_int = sys.maxsize+1  print("maxint :"+str(max_int)+" - "+str(type(max_int)))print("maxint - 1 :"+str(max_int)+" - "+str(type(min_int)))  # the data type is represented as intprint("maxint + 1 :"+str(max_int)+" - "+str(type(long_int)))
maxint :9223372036854775807 - <class 'int'>
maxint - 1 :9223372036854775807 - <class 'int'>
maxint + 1 :9223372036854775807 - <class 'int'>

Python-sys
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Python Classes and Objects
How to drop one or multiple columns in Pandas Dataframe
Defaultdict in Python
Python | Get unique values from a list
Python | os.path.join() method
Create a directory in Python
Python | Pandas dataframe.groupby()