How to Get Max Int in Python?

Python 2 has two integer data types, int and long, which are similar to Python 3’s int data type. “Integer” in Python 3 has no maximum limit, so it can handle as large values as memory allows. To check the maximum limit of int in the current interpreter, the “sys.maxsize” and the “Numpy” module are used in Python.

This post will demonstrate the methods to get the maximum integer value in Python with the following content:

What is Python Max Int?

The maximum value represented by an integer in Python is known as “Max Int”. For example, the  “2^31-1” for a 32-bit system and “2^63-1” for a 64-bit system is the maximum integer value for “32” and “64” bit integers.

Method 1: Using sys.maxsize Function

The “sys.maxsize” is a built-in constant in the Python sys module that returns the maximum value for an integer on the current platform. Let’s practice it through examples:

Example 1: How to Get Maximum Integer Value Using “sys.maxsize” Function?

In the below code, the “sys.maxsize” function of the “sys” module is used to fetch the max and min integer values. Here is a code example:

Code:

import sys
print(sys.maxsize)

In the above code, the “sys” module is imported, and “sys.maxsize” is used to get the max int in Python.

Output:

The above output shows the max Python integer value.

Example 2: How to Get Minimum Integer Value Using “sys.maxsize” Function?

In the below code, the “~sys.maxsize” function is used to get the minimum value of the integer:

Code:

import sys
print(~sys.maxsize)

In the above code, the “sys” module is imported and “~sys.maxsize” is used to get the minimum int in Python.

Output:

The above output shows the minimum int in Python.

Method 2: Using Numpy Module

The Numpy Module also provides a function named “np.iinfo()” to get the maximum or the minimum integer:

Example 1: Maximum Integer Value

In the code given below, the “np.iinfor()” is used with “.max” to find the max int of “np.int64” in Python.

Code:

import numpy as np
print(np.iinfo(np.int64).max)

In the above code, the “np.iinfo()” function accepts the 64-bit integer, i.e., “np.int64” as an argument and will return the maximum integer value.

Output:

The above output shows the maximum integer value of the given 64-bit integer bit in Python.

Example 2: Minimum Integer Value

In the below example, “np.iinfo()” is utilized with “.min” to find the minimum integer of the given “np.int64” integer bit:

Code:

import numpy as np
print(np.iinfo(np.int64).min)

In the above code, the “np.iinfo()” function accepts the 64-bit integer “np.int64” as an argument and returns the minimum integer.

Output:

This way, you can find the minimum int using the np.iinfo() function.

Conclusion

To find the max int, the “sys.maxsize()” and the “np.iinfor()” functions are used in Python. The “~sys.maxsize” is used to find the minimum integer. The “sys” module provides a function “sys.maxsize()” to calculate the max and min integers. The “np.iinfo()” function accepts any integer bit, such as “int-8”, “int-16”, “Int-32”, etc., as an argument and returns the maximum or minimum integer. This post presented a straightforward tutorial on how to find the max and min int in Python using suitable examples.

Over 500 Million Views on Our Blogs and YouTube! We’re inviting 10 businesses to partner with us and grow together. Click here to join us! ×