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.
Note: This is a depreciate version and will not work on Python 3, for Python 3 you can read the article: sys.maxsize in Python.
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))
OutputThe 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))
OutputThe 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)))
Outputmaxint :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.
Python
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.
Python
# 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)))
Outputmaxint :9223372036854775807 - <class 'int'>
maxint - 1 :9223372036854775807 - <class 'int'>
maxint + 1 :9223372036854775807 - <class 'int'>
Similar Reads
sys.maxsize in Python
maxsize attribute of the sys module fetches the largest value a variable of data type Py_ssize_t can store. It is the Python platform's pointer that dictates the maximum size of lists and strings in Python. The size value returned by maxsize depends on the platform architecture: 32-bit: the value wi
2 min read
max() and min() in Python
This article brings you a very interesting and lesser-known function of Python, namely max() and min(). Now when compared to their C++ counterpart, which only allows two arguments, that too strictly being float, int or char, these functions are not only limited to 2 elements, but can hold many eleme
3 min read
np.nanmax() in Python
numpy.nanmax()function is used to returns maximum value of an array or along any specific mentioned axis of the array, ignoring any Nan value. Syntax : numpy.nanmax(arr, axis=None, out=None, keepdims = no value) Parameters : arr : Input array. axis : Axis along which we want the max value. Otherwise
2 min read
SymPy | Permutation.max() in Python
Permutation.max() : max() is a sympy Python library function that returns the maximum value in the permutation. Syntax : sympy.combinatorics.permutations.Permutation.max() Return : maximum value in the permutation. Code #1 : max() Example # Python code explaining # SymPy.Permutation.max() # importin
1 min read
Use of min() and max() in Python
Prerequisite: min() max() in Python Let's see some interesting facts about min() and max() function. These functions are used to compute the maximum and minimum of the values as passed in its argument. or it gives the lexicographically largest value and lexicographically smallest value respectively,
2 min read
numpy.maximum() in Python
numpy.maximum() function is used to find the element-wise maximum of array elements. It compares two arrays and returns a new array containing the element-wise maxima. If one of the elements being compared is a NaN, then that element is returned. If both elements are NaNs then the first is returned.
2 min read
Max Heap in Python
A Max-Heap is a Data Structure with the following properties: It is a Complete Binary Tree.The value of the root node must be the largest among all its descendant nodes, and the same thing must be done for its left and right sub-tree also.How is Max Heap represented? A max Heap is a Complete Binary
6 min read
Python - max() function
Python max() function returns the largest item in an iterable or the largest of two or more arguments. It has two forms. max() function with objectsmax() function with iterablePython max() function With ObjectsUnlike the max() function of C/C++, the max() function in Python can take any type of obje
4 min read
Python min() Function
Python min() function returns the smallest of the values or the smallest item in an iterable passed as its parameter. Example: Find Python min integer from the list [GFGTABS] Python numbers = [23,25,65,21,98] print(min(numbers)) [/GFGTABS]Output 21Python min() Function Syntaxmin(a, b, c, ..., key=fu
4 min read
SymPy | Permutation.min() in Python
Permutation.min() : min() is a sympy Python library function that returns the minimum value in the permutation. Syntax : sympy.combinatorics.permutations.Permutation.min() Return : minimum value in the permutation Code #1 : min() Example # Python code explaining # SymPy.Permutation.min() # importing
1 min read