sys.maxint in Python (original) (raw)

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

**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)

`

**Explanation:

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:

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:

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)

`

**Explanation:

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: