Python

Python Max integer

The integer number of different sizes exists in the standard programming language, such as short integer, medium integer, long integer, etc. There is no separate datatype for short and long integers in python 3. The int in Python3 is equivalent to long in Python 2. There is no maximum limit of an integer value in Python 3, and it can store large integer numbers based on the available memory. The maxsize property of the sys module is used to define a maximum-sized integer number in python 3. The value of sys.maxsize is 231-1 for the 32-bit computer, and the value of sys.maxsize is 263-1 for the 64-bit computer. The ways of using maximum integer numbers in Python 3 have been shown in this tutorial by using different python scripts.

Example-1: Find the value and type of sys.maxsize

Create a python file with the following script that will print the type of the computer-based on the value of sys.maxsize, and print the type and value of the sys.maxsize. If the value return by the sys.maxsize is equal to 2**63 – 1, then the message, ‘It is a 64-bit computer,’ will be printed. If the value return by the sys.maxsize is equal to 2**31 – 1, then the message, ‘It is a 32-bit computer,’ will be printed.

#Import sys module
import sys

#Check the sys.maxsize value for 64-bit environment
if sys.maxsize == 2**63 - 1:
   print("It is a 64-bit computer.")

#Check the sys.maxsize value for 32-bit environment
if sys.maxsize == 2**31 -1:
   print("It is a 32-bit computer.")

#Print the type of the sys.maxsize
print("The type of sys.maxsize is",type(sys.maxsize))

#Print the value of sys.maxsize
print("The value of sys.maxsize is",sys.maxsize)

Output:

The following output will appear after executing the above script. The output shows that the computer is 64-bit where the script has been executed. The type of sys.maxsize is int, and the size of sys.maxsize is 9223372036854775807.

Example-2: Find the binary, octal, and hexadecimal values of sys.maxsize

Create a python file with the following script to print the binary, octal and hexadecimal values of the sys.maxsize. The bin() function has been used to get the binary value. The oct() function has been used to get the octal value. The hex() function has been used to get the hexadecimal value.

#Import sys module
import sys

#Print the binary value of sys.maxsize
print("Binary: ",bin(sys.maxsize))

#Print the octal value of sys.maxsize
print("Octal: ",oct(sys.maxsize))

#Print the hexadecimal value of sys.maxsize
print("Hexadecimal: ",hex(sys.maxsize))

Output:
The following output will appear after executing the above script. The binary value is started by ‘0b…’. The octal value is started by ‘0o…’. The hexadecimal value is started by ‘0x…’.

Example-3: Calculate the power that contains the long integer

The sys.maxsize is not the maximum possible value of int in Python 3, and it is possible to handle a large number of integer values based on the available memory. Create a python file with the following script to calculate 9 to the power 999, a very long integer. If the memory is available to store the output of the calculation, then the value of 9999 will be printed. Next, the type of output value will be printed.

#Calculate 9 to the power 999
number = 9**999
#Print the output of the power
print("The output of 9 to the power 999 is:\n",number)
#Print the type of the output
print("The type of the output is:\n",type(number))

Output:
The following output will appear after executing the above script. The output shows the value of 9999, and the output type is an integer.

Example-4: Find the minimum value in the list using sys.maxsize

Create a PHP file with the following script to find the minimum value from a list of numbers using the sys.maxsize property. A list of 6 numeric values has been declared here. The minimum value has been initialized by sys.maxsize to the variable named minval, and it has been used to find out the minimum value of the list. Next, a ‘for’ loop is used to iterate each element of the list and compare the value with the value of the minval variable. If the current value is less than the minval, then the minval will be re-initialized with the current value of the list. The minimum value of the list will be stored in the minval after completing the iteration of the ‘for’ loop.

#Import sys module
import sys

#Declare a list of numbers
listdata = [89567888, 400000, 9845000, 7645, 812000, 94534]

#Set the sys.maxsize as maximum value
minval = sys.maxsize

#Iterate the list data using loop
for index in range(0, len(listdata)):
    #Update the minimum value based on comparison
    if listdata[index] < minval:
        minval = listdata[index]

#Print all data from the list
print("The list values are:\n",listdata)
#Print the minimum value of the list
print("The minimum value in the list is:",minval)

Output:
The following output will appear after executing the above script. The minimum value of the list is 7645, which has been printed in the output.

Example-5: Maximum integer value information with size

Create a python file with the following script that will display the integer information of the sys.int_info and the size of sys.maxsize.

#Import sys module
import sys

#Print the integer information
print("\nInteger value information: ",sys.int_info)
#Print the maximum integer value based on the system
print("\nMaximum integer size: ",sys.maxsize)

Output:
The following output will appear after executing the above script. The output shows that integer information and the value of the sys.maxsize.

Conclusion:

Different ways to find out the maximum integer value in python 3 have been shown in this tutorial using different examples. The maximum integer value is not fixed in python 3, but the value of sys.maxsize is fixed based on the computer.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.