How to get the Max Int in Python
Table of Contents
Every programming language has a limit to how large an integer can be.
This is because there is a set number of bits that can be used to represent a number, and so those bits only have so many different permutations.
Typically, when this number is exceeded, the number is called an overflow, and will go negative if the number is signed.
In Python 2, the maximum value for an integer is limited to 2 ** 63 - 1.
That is to say the number 2, raised to the power of 63, minus 1.
In Python 3, the maximum value is determined by the machine that the code is running on.
In this post, we'll learn how to get the maximum value for an integer in Python at runtime.
Getting the maximum value for an integer
The easiest way to get the maximum value for an integer is to use the built-in function sys.maxint
.
To use sys
functions, you need to import the sys
module.
PYTHONimport sys
print (sys.maxint)
BASH9223372036854775807
Keep in mind that as mentioned before, this is the maximum value for an integer in Python 2.
For Python 3, this doesn't apply because the value is unbounded, determined by the machine running the code.
Conclusion
In this post, we saw how to get the maximum value for an integer in Python at runtime when on Python 2.
This is different on Python 3 which just relies on the under-lying system to determine the maximum value.
Hopefully, this has been helpful to you. Thanks for reading!
- Managing PHP Dependencies with Composer
- How to Set Up Cron Jobs in Linux
- How to deploy a .NET app using Docker
- How to Scrape the Web using Node.js and Puppeteer
- Build a Real-Time Chat App with Node, Express, and Socket.io
- Getting Started with Moment.js
- Learn how to build a Slack Bot using Node.js
- Using Push.js to Display Web Browser Notifications
- Building a Real-Time Note-Taking App with Vue and Firebase
- Getting Started with React
- Using Axios to Pull Data from a REST API
- How To Create a Modal Popup Box with CSS and JavaScript