Get Maximum & Minimum values for ints in Python

In this python tutorial, you will learn how to get Maximum and Minimum values for an integer in Python.

Table Of Contents

Let’s dive into the tutorial.

Get maximum and minimum int values using sys.maxint in Python 2.0

Up to Python 2.0, to get the maximum and minimum integer we can use the sys.maxint() method available in the sys module. To use this method, we have to import the sys module.

import sys

Syntax to get the maximum value of int:

sys.maxint

Example: Get maximum Integer

import sys

# Get maximum integer
maxIntValue = sys.maxint

print(maxIntValue)

Output:

9223372036854775807

We can see that the 9223372036854775807 is the maximum integer value. We ran this program with python version 2.7.18. It might not run with python version 3 onwards. In this next section of article we will discuss ways to get max value of int in python3.

In order to get the minimum integer, there are two ways.

Syntax to get minimum int:

-sys.maxint - 1

(or)

~sys.maxint

Example: Get minimum Integer

import sys

# Get minimum integer value
minIntValue = -sys.maxint - 1

print(minIntValue)

# Get minimum integer value
minIntValue = ~sys.maxint

print(minIntValue)

Output:

-9223372036854775808
-9223372036854775808

We can see that the -9223372036854775808 is the minimum integer. We ran this program with python version 2.7.18. It might not run with python version 3 onwards. In this next section of article we will discuss ways to get min value of int in python3.

Get maximum and minimum values of int using sys.maxsize in Python3

From Python 3.0 onwards, to get the maximum and minimum integer we can use the sys.maxsize() method available in the sys module. To use this method, we have to import the sys module.

import sys

Syntax to get maximum int:

sys.maxsize

Example: Get maximum Integer

import sys

# Get maximum integer value
print(sys.maxsize)

Output:

9223372036854775807

We can see that 9223372036854775807 is the maximum integer. In order to get the minimum integer, there are two ways.

Syntax to get minimum int:

-sys.maxsize - 1

(or)

~sys.maxsize

Example: Get minimum Integer

import sys

# get minimum integer
print(-sys.maxsize - 1)

# get minimum integer
print(~sys.maxsize)

Output:

-9223372036854775808
-9223372036854775808

We can see that -9223372036854775808 is the minimum integer.

Get maximum and minimum values of int using numpy module

The numpy.iinfo() is the method available in numpy used to display the system size bit limits. It returns maximum and minimum integer values for different sizes of integers.

Syntax:

numpy.iinfo(numpy.int(size))

where size refers to the integer system size.

Example:

In this example, we will return maximum and minimum values of an integer using numpy.iinfo().

import numpy

# get machine limits for int-8 size
print(numpy.iinfo(numpy.int8))

# get machine limits for int-16 size
print(numpy.iinfo(numpy.int16))

# get machine limits for int-32 size
print(numpy.iinfo(numpy.int32))

# get machine limits for int-64 size
print(numpy.iinfo(numpy.int64))   

Output:

Machine parameters for int8
---------------------------------------------------------------
min = -128
max = 127
---------------------------------------------------------------

Machine parameters for int16
---------------------------------------------------------------
min = -32768
max = 32767
---------------------------------------------------------------

Machine parameters for int32
---------------------------------------------------------------
min = -2147483648
max = 2147483647
---------------------------------------------------------------

Machine parameters for int64
---------------------------------------------------------------
min = -9223372036854775808
max = 9223372036854775807
---------------------------------------------------------------

We can see that,

  1. For int-8, the maximum integer is 127 and the minimum integer is -128
  2. For int-16, the maximum integer is 32767 and the minimum integer is -32768
  3. For int-32, the maximum integer is 2147483647 and the minimum integer is -2147483648
  4. For int-64, the maximum integer is 9223372036854775807 and the minimum integer is -9223372036854775808

We can also return maximum and minimum integers separately using max and min functions.

Syntax:

numpy.iinfo(numpy.int(size)).max
numpy.iinfo(numpy.int(size)).min

Let’ ‘s see the example.

import numpy

# Get maximum value of int8
print(numpy.iinfo(numpy.int8).max)

# Get maximum value of int16
print(numpy.iinfo(numpy.int16).max)

# Get maximum value of int32
print(numpy.iinfo(numpy.int32).max)

# Get maximum value of int64
print(numpy.iinfo(numpy.int64).max)

# Get minimum value of int8
print(numpy.iinfo(numpy.int8).min)

# Get minimum value of int16
print(numpy.iinfo(numpy.int16).min)

# Get minimum value of int32
print(numpy.iinfo(numpy.int32).min)

# Get minimum value of int64
print(numpy.iinfo(numpy.int64).min)

Output:

127
32767
2147483647
9223372036854775807
-128
-32768
-2147483648
-9223372036854775808

Summary

In this tutorial, we have seen how to return a maximum and minimum integer value, in the before and latest versions using sys module. The maxint is used in the python 2.0 and maxsize is used in the python 3.0 version onwards. Also, we noticed that using ~ and – operators, we can get the minimum integer from the maxsize, and maxint attributes. Also we found that based on the system compiler or machine type, maximum and minimum values are returned using numpy.iinfo() module in Python. Happy Learning.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top