Python тип int максимальное число

Python Maximum/Minimum Integer Value (with Examples)

In Python, you can use sys.maxsize from the sys module to get the maximum and minimum integer values.

The sys.maxsize gives you the maximum integer value and its negative version -sys.maxsize — 1 gives you the minimum integer value.

import sys # Get the maximum integer value max_int = sys.maxsize # Get the minimum integer value min_int = -sys.maxsize - 1 # Print the values print(f"The maximum integer value is ") print(f"The minimum integer value is ")

Notice that Python 3.0+ doesn’t limit the int data type and there’s no maximum/minimum value. But to get the practical maximum integer value for a single integer word in your operating system, use sys.maxsize as shown above.

This guide teaches you how to get the maximum and minimum integer values in Python.

You will understand why maximum and minimum integer values exist in the first place. You’ll also learn how to get the maximum/minimum integer values in Python versions earlier than 3.0 as well as Python 3.0+. Besides, you’ll learn how to get the maximum/minimum integers in NumPy.

Let’s jump into it!

Integer Value Extrema

Computer memory is limited. A typical operating system uses a 32-bit or 64-bit system for representing numbers. This means there are 2³² or 2⁶⁴ numbers the system can represent.

Notice that the maximum/minimum integer size restriction is not a Python-only thing as it depends on the capabilities of the device, not the programming language.

Let’s take a closer look at what limits the size of an integer in general.

Why Is There a Maximum/Minimum for Integers?

32-bit integer maximum and minimum values

A maximum integer is the maximum number that a binary store can hold. Many operating systems use 32 bits for storage. This means you have to fit any number that you want to use in those 32 bits.

A bit is a binary digit whose value is either 0 or 1. If you form a line of these bits, you can get a number of different combinations of 0s and 1s.

For example, if you have 2 bits, you have 2² = 4 possible combinations:

And for N bits, you have 2^N possible combinations.

A computer that uses 32 bits to represent numbers has a total number of 2³² = 4,294,967,296 possible combinations. This means the computer can represent 2³² numbers in total such that each combination of bits corresponds to a number in the decimal number system.

But as you know, you don’t only have positive numbers but also negatives and zero. But bits don’t understand negative signs. Instead, a computer can use 1 as a negative sign and 0 as a positive sign.

Читайте также:  Site navigation menu html

In a 32-bit sequence, this means that the sign is denoted by the left-most bit (1 for negative, 0 for positive values). Because one bit is used as a sign, it leaves 31 bits for representing the actual numbers.

This means the biggest integer in a 32-bit system is 0 followed by 31 1’s. In other words, 2³⁰ + 2²⁹ + … + 2² + 2¹ + 2⁰. This is 2³¹ – 1 or 2,147,483,647. So you can count from 0 to 2,147,483,647 using this type of 32-bit signed integer system.

When it comes to negative values, the idea is exactly the same. The smallest negative value is 1 followed by 31 1’s, that is, 2³⁰ + 2²⁹ + … + 2² + 2¹ + 2⁰. As a negative value, this is -2,147,483,647. But remember, because the 0 value is already included in the range of countable positive numbers, we start counting the negative values from -1 instead of 0. This means the smallest possible negative value in the 32-bit signed integer system is actually one less than -2,147,483,647, that is, -2,147,483,648.

And if the computer uses 64 bits for storing numbers, the idea is the same.

The maximum value of a 64-bit signed integer is 2⁶³ – 1 = 9,223,372,036,854,775,807 and the minimum value is -(2⁶³ – 1) – 1 = -9,223,372,036,854,775,808.

To take home, the maximum and minimum values for integers in programming languages are determined by the amount of memory that is allocated for storing them and the type of integer that is being used. These values are important because they determine the range of values that an integer variable can store, and they can affect the precision and accuracy of calculations that involve integers.

Python Maximum Integer Value

To obtain the maximum value of the integer data type, use the sys.maxsize .

import sys # Get the max integer value max_int = sys.maxsize print(f"The maximum integer value is ")

Python Minimum Integer Value

To obtain the minimum value of the integer data type, use the negative sys.maxsize value and subtract 1 from it to account for 0 being in the positive value range.

import sys # Get the min integer value min_int = -sys.maxsize - 1 print(f"The minimum integer value is ")

Python 3+ ‘int’ Type Has No Limits!

As of Python 3, the int type is unbound. This means there’s no limit to how big a number you can represent using the int type.

Back in Python versions earlier than 3.0, the int type was bound to be between [-2⁶³, 2⁶³ – 1]. If you wanted to use a number larger or smaller than what is in the range, you’d have to use the long data type. As a matter of fact, a conversion from int to long takes place automatically.

As an example, let’s try printing some large numbers in Python 2 where the int type was still bounded:

print(9223372036854775807) print(9223372036854775808)
9223372036854775807 9223372036854775808L

Notice the ‘L’ at the end of the second number. Because 9223372036854775808 is bigger than the maximum int value, it automatically became a long which Python denotes by adding ‘L’ to the end.

But in Python 3, the int type is unbounded. Essentially, what was long in Python 2 is now int in Python 3+. Besides, the ‘L’ letter is no longer added to the end of big integers!

Читайте также:  PHP Insert Update Delete with Vue.js

Let’s repeat the previous example in Python 3:

print(9223372036854775807) print(9223372036854775808)
9223372036854775807 9223372036854775808

Even though there’s no limit to how big an integer can be in Python, the sys.maxsize gives you the upper bound for practical lists or strings. The maximum value works as a sentinel value in algorithms, so even though it’s not a hard limit in Python integers, it indicates the maximum word size used to represent integers behind the scenes.

If this makes no sense, let’s see what happens if you try to use an integer larger than sys.maxsize to access a list element:

l = [1,2,3,4,5,6] value = l[10000000000000000000000000000000000]
Traceback (most recent call last): File "", line 1, in IndexError: cannot fit 'int' into an index-sized integer

The error says that the integer index you’re using to access the list elements is too big to be an index. This happens because of the limitations of your operating system. A Python index must still be an integer bounded by the 32-bit or 64-bit limits.

So even though the int is unbounded in Python, the underlying operating system still uses either 32 or 64 bits to represent numbers.

Thanks for reading. Happy coding!

Read Also

Источник

Python Max Int | What’s the Maximum Value of int Data Type in Python

Python Max Int

As 70% of the folks who start learning python come from C, C++, or Java background. So while learning the python programming language many times, a question arises in our mind what’s the python max int or the maximum value of integer data type in python. If you have the same query in your mind, don’t worry; you are in the right place, and we will clear all of your doubts in this article.

When it comes to a max int value in python, this query doesn’t apply to the python programming language. As we all know, python is a cool and modern programming language. So while talking about python, we can say the int data type in python is unbounded to any limits and restrictions.

Let’s see practical examples to prove that there are unlimited precision and no explicitly defined limits for the maximum value of an integer in python.

Finding Maximum Possible Value of an Integer [Python Max Int] in Python 3

To check if there is any restraint or limit in Python max int value. We will take an example and try to print an integer value as large as possible. So, let’s directly jump into the example.

Example to check if there is any Maximum integer value [Python Max Int] in python 3

n = 99999999999999999999999999999999999999999999999999999999 n = n + 1 print (n) print(type(n))
100000000000000000000000000000000000000000000000000000000

From the above example, it’s now clear that there is no limit for max int in Python. The number of bits does not restrict the value of an integer, but it depends on the size of the available memory.

Finding Maximum Possible Value of an Integer [Python Max Int] in Python 2

n = 999 print(type(n)) n = 99999999999999999999999999999999999999999999999999999999 print(type(n))

So from the above example, it’s now clear that there is some maximum value of integer (Python Max int) in Python 2. To find out the maximum value of an integer in Python 2, we will use a constant sys.maxint.

Читайте также:  Php скрипт мой ip адрес

Let’s see an example of sys.maxint constant in python 2.

Example to find maximum value [Python Max Int] using sys.maxint in python 2.7

import sys print sys.maxint print type(sys.maxint) print type(sys.maxint + 1)

Explanation:

From the above example, we can say that there is some limit in the integer size when we talk about Python 2.
But we don’t need to bother about the size of integer or any other data type. As Python seamlessly switches from plain integer to long integers once you exceed this value.

As a side note, in Python 3, there’s only 1 type”int” for all sorts of integers. In Python 2.7. There are two distinct types”int” (which is 32-bit) and”long int” which are the same as”int” of Python 3.x, i.e., can store arbitrarily large amounts.

Does sys.maxint Still Works with Python 3?

As we already discussed in the above sections, there is no limit for integers in python 3. So, the sys.maxint constant was eliminated as there is no longer a limit to the value of integers. However, sys.maxsize may be utilized as an integer more significant than any sensible list or series index. It conforms to the execution’s”natural” integer size and is typically the same as sys.maxint in previous releases on the same stage (assuming the same build options).

Checking by example if sys.maxint works in python 3

import sys print(type(sys.maxint))
Traceback (most recent call last): File "", line 1, in AttributeError: module 'sys' has no attribute 'maxint'

So, from the above example we have proved that there is no sys.maxint available in python 3. However we can use sys.maxsize in place of sys.maxint.

Using sys.maxsize to Find Python Max Int in Python 3

As from the above we can now conclude that in Python 3 there is not constant named sys.maxint. So, instead of this we are using another constant sys.maxsize which work quite similar to sys.maxint.

Let’s directly jump into the example and try to find the maximum value of integer using sys.maxsize.

Example of seeking the maximum value of an integer [Python Max Int] in python 3 [sys.maxsize]

import sys print(type(sys.maxsize)) print(type (sys.maxsize + 1))

Explanation:

In the above example we can clearly see that if we are using Python 3 then there is no maximum value for the integer. Even after incrementing the sys.maxsize constant by 1, the data type remains of type integer. So, now we can say that in python 3 int and long are actually merged and the value has no such importance.

Must Read

Conclusion

So if you make it till the end, I am pretty sure you now be able to understand the concept behind the maximum value in Python or Python max int. I think you might also want to know Ways in Python to Sort the List of Lists. If yes there is an awesome tutorial available in our library of tutorials do check it out.

Still have any doubts or questions do let me know in the comment section below. I will try to help you as soon as possible.

Happy Pythoning!

Источник

Оцените статью