Open In App

sys.maxint in Python

Last Updated : 09 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Python 2, sys.maxint refers to the maximum value an integer can hold on a given platform. This value is:

  • 2³¹ - 1 = 2147483647 on a 32-bit system
  • 2⁶³ - 1 = 9223372036854775807 on a 64-bit system

sys.maxint was typically used as a very large number in algorithms requiring an upper bound, like finding minimum values in a list.

Note: sys.maxint has been removed in Python 3. Use sys.maxsize as a practical alternative.

Examples of sys.maxint

Example 1: Using sys.maxint to Find the Minimum in a List

This example demonstrates how sys.maxint can be used to initialize a variable with a very large number when finding the smallest value in a list.

Python
import sys

li = [1, -22, 43, 89, 2, 6, 3, 16]
curr_min = sys.maxint

for i in li:
    if i < curr_min:
        curr_min = i

print(curr_min)

Output
-22

Explanation:

  • curr_min = sys.maxint: initializes with the largest possible integer.
  • loop iterates and updates curr_min when a smaller number is found.

Example 2: Behavior Around sys.maxint

This example shows how Python 2 handles integers when they exceed sys.maxint.

Python
import sys

max_int = sys.maxint
min_int = sys.maxint - 1
long_int = sys.maxint + 1

print("maxint:", max_int, "-", type(max_int))
print("maxint - 1:", min_int, "-", type(min_int))
print("maxint + 1:", long_int, "-", type(long_int))

Output
('maxint:', 9223372036854775807, '-', <type 'int'>)
('maxint - 1:', 9223372036854775806, '-', <type 'int'>)
('maxint + 1:', 9223372036854775808L, '-', <type 'long'>)

Explanation:

  • Adding 1 to sys.maxint results in a long type.
  • Python 2 differentiates between int and long types.

Example 3: Using sys.maxint in Python 3 (Causes Error)

Attempting to use sys.maxint in Python 3 throws an error because the attribute no longer exists.

Python
import sys

li = [1, -22, 43, 89, 2, 6, 3, 16]
curr_min = sys.maxint

for i in li:
    if i < curr_min:
        curr_min = i

print(curr_min)

Output :

AttributeError: module 'sys' has no attribute 'maxint'

Explanation:

  • sys.maxint was removed in Python 3.
  • Python 3 integers are of arbitrary precision, so there's no fixed maximum.

Alternative in Python 3: sys.maxsize

Example 4: Using sys.maxsize to Find Minimum Value

sys.maxsize can be used as an upper bound replacement for sys.maxint in Python 3.

Python
import sys

li = [1, -22, 43, 89, 2, 6, 3, 16]
curr_min = sys.maxsize

for i in li:
    if i < curr_min:
        curr_min = i

print(curr_min)

Output
-22

Explanation:

  • sys.maxsize: returns the maximum value a variable of type Py_ssize_t can hold.
  • Used for practical limits in lists, strings, and other containers.

Example 5: Behavior Beyond sys.maxsize

This shows how Python 3 handles integers beyond sys.maxsize.

Python
import sys

max_int = sys.maxsize
min_int = sys.maxsize - 1
long_int = sys.maxsize + 1

print("maxsize:", max_int, "-", type(max_int))
print("maxsize - 1:", min_int, "-", type(min_int))
print("maxsize + 1:", long_int, "-", type(long_int))

Output
maxsize: 9223372036854775807 - <class 'int'>
maxsize - 1: 9223372036854775806 - <class 'int'>
maxsize + 1: 9223372036854775808 - <class 'int'>

Explanation:

  • Python 3 treats all integers as type int, even beyond maxsize.
  • No separate long type; integers are of arbitrary length.

Next Article
Practice Tags :

Similar Reads