Related Articles

# sys.maxint in Python

• Last Updated : 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 list``li ``=` `[``1``, ``-``22``, ``43``, ``89``, ``2``, ``6``, ``3``, ``16``]`` ` `# assigning a larger value manually``curr_min ``=` `999999`` ` `# loop to find minimum value``for` `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
```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.

## Python

 `# import the module``import` `sys`` ` `# initializing the list``li ``=` `[``1``, ``-``22``, ``43``, ``89``, ``2``, ``6``, ``3``, ``16``]`` ` `# assigning a larger value with ``# maxint constant``curr_min ``=` `sys.maxint`` ` `# loop to find minimum value``for` `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

```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 module``import` `sys`` ` `max_int ``=` `sys.maxint``min_int ``=` `sys.maxint``-``1``long_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)))`
Output
```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 list``li ``=` `[``1``, ``-``22``, ``43``, ``89``, ``2``, ``6``, ``3``, ``16``]`` ` `# assigning a larger value with maxint constant``curr_min ``=` `sys.maxint`` ` `# loop to find minimum value``for` `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 module``import` `sys`` ` `# using sys.maxsize``max_int ``=` `sys.maxsize``min_int ``=` `sys.maxsize``-``1``long_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 int``print``(``"maxint + 1 :"``+``str``(max_int)``+``" - "``+``str``(``type``(long_int)))`
Output
```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

My Personal Notes arrow_drop_up