What is the range of a 32-bit integer in Python?

Brenda 81 Published: 12/05/2024

What is the range of a 32-bit integer in Python?

I'm happy to help you with that!

In Python, integers are arbitrarily large and can grow without bound. However, the size of an integer depends on the platform it's running on.

For most systems, including Windows, macOS, and Linux, a 32-bit integer in Python is represented as an int type variable which has a range of:

-4,294,967,295 to +4,294,967,296 (2^31 -1)

This means that the maximum value for a 32-bit integer in Python is 2,147,483,647. If you try to assign a value larger than this to a variable, it will automatically get converted to a long or long integers.

Now let's explore why we say that:

In Python, integers are implemented using C long ints on most platforms. These have a maximum size of 2^31-1. The actual range depends on the platform you're running on.

For example, on 64-bit Windows, the int type is typically represented as an __int64 which has a much larger range:

-9,223,372,036,854,775,807 to +9,223,372,036,854,775,808 (2^63 -1)

On Linux and macOS, where Python's interpreter is often run on a 32-bit platform, the maximum int value remains at 2^31-1.

As for how we get this range:

The number of bits in an integer is usually defined as ctypes.c_long().bitfield(). You can use this to determine whether you're running on a 64-bit or 32-bit machine.

If it's not already set, then the maximum value should be adjusted. We adjust the maximum int value based on what Python’s C API says:

-4,294,967,295 if 'long' is used (2^31 -1)

-9,223,372,036,854,775,807 if 'PyLong_AsLLong('long long') is used (2^63 -1)

Lastly, we can easily test the range of integers in Python by running some code:

# check the size of an integer

x = 0

try:

x += 10**30

except OverflowError as e:

print(f"Overflow Error: {e}")

else:

print("Integer size is larger than expected")

print(f"x = {x}")

print("The range of a 32-bit integer in Python is -2^31 to +2^31-1")

test the maximum value

max_val = 10**9

while True:

try:

x += max_val

except OverflowError as e:

print(f"Maximum integer value exceeded: {e}")

break

else:

if x > max_val:

print("The range of a 32-bit integer in Python is -2^31 to +2^31-1")

This script tests the range by checking whether an integer exceeds its maximum value or not.

Python int range example

Here's an example of how you can use the range function in Python to generate a sequence of integers:

# Create a range object that generates numbers from 1 to 10

my_range = range(1, 11)

Print each number in the range

for num in my_range:

print(num)

When you run this code, it will output the following:

1

2

3

4

5

6

7

8

9

10

In this example, we use the range function to create a range object that generates numbers from 1 to 10. The range function takes three arguments: the starting value, the stopping value, and an optional step value. By default, the step value is 1.

We then print each number in the range using a for loop. The for loop iterates over each number in the range, assigning it to the variable num. We print the value of num inside the loop, which results in each number being printed on a separate line.

Here's another example that demonstrates how you can use the range function to generate numbers with a non-default step:

# Create a range object that generates numbers from 0 to 9 with a step of 2

my_range = range(0, 10, 2)

Print each number in the range

for num in my_range:

print(num)

When you run this code, it will output the following:

0

2

4

6

8

In this example, we create a range object that generates numbers from 0 to 9 with a step of 2. This means that the numbers in the range are spaced evenly apart by 2 units. We then print each number in the range using a for loop, which results in the numbers being printed on separate lines.

The range function is a powerful tool in Python that allows you to generate sequences of integers with ease. You can use it to create ranges with default steps or custom steps, and you can use it to iterate over each number in the range using a for loop.