sys.maxint in Python
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.
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.
sys module in Python
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.
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.
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.
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.
maxint :9223372036854775807 - <class 'int'> maxint - 1 :9223372036854775807 - <class 'int'> maxint + 1 :9223372036854775807 - <class 'int'>
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course