- Get Maximum & Minimum values for ints in Python
- Get maximum and minimum int values using sys.maxint in Python 2.0
- Frequently Asked:
- Get maximum and minimum values of int using sys.maxsize in Python3
- Get maximum and minimum values of int using numpy module
- Summary
- Related posts:
- Share your love
- Leave a Comment Cancel Reply
- Terms of Use
- Disclaimer
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.
Syntax to get the maximum value of int:
Frequently Asked:
Example: Get maximum Integer
import sys # Get maximum integer maxIntValue = sys.maxint print(maxIntValue)
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:
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)
-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.
Syntax to get maximum int:
Example: Get maximum Integer
import sys # Get maximum integer value print(sys.maxsize)
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:
Example: Get minimum Integer
import sys # get minimum integer print(-sys.maxsize - 1) # get minimum integer print(~sys.maxsize)
-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.
where size refers to the integer system size.
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))
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 ---------------------------------------------------------------
- For int-8, the maximum integer is 127 and the minimum integer is -128
- For int-16, the maximum integer is 32767 and the minimum integer is -32768
- For int-32, the maximum integer is 2147483647 and the minimum integer is -2147483648
- 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.
numpy.iinfo(numpy.int(size)).max numpy.iinfo(numpy.int(size)).min
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)
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.
Related posts:
Share your love
Leave a Comment Cancel Reply
This site uses Akismet to reduce spam. Learn how your comment data is processed.
Terms of Use
Disclaimer
Copyright © 2023 thisPointer
To provide the best experiences, we and our partners use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us and our partners to process personal data such as browsing behavior or unique IDs on this site and show (non-) personalized ads. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Click below to consent to the above or make granular choices. Your choices will be applied to this site only. You can change your settings at any time, including withdrawing your consent, by using the toggles on the Cookie Policy, or by clicking on the manage consent button at the bottom of the screen.
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.