Featureintro
The int type
Also called Python int, Python integers, arbitrary-precision integers, bignum, int(), int.to_bytes, int.bit_length.
int holds whole numbers with no fixed size, so it never overflows. int() converts numbers and strings in bases 2 to 36, and bitwise operators act as if negative numbers had infinite sign bits.
- Language
- Python
Synopsis
x = 2 ** 100
print(x)
print(x.bit_length(), type(x).__name__)
print(int("ff", 16), int("0b101", 0), int(" -12_345\n"), int(-3.99))
1267650600228229401496703205376
101 int
255 5 -12345 -3
2 ** 100 is exact, with no overflow. int() reads strings in other bases and truncates a float toward zero.
Semantics
r1. "Integers have unlimited precision."1 An int represents numbers "in an unlimited range, subject to available (virtual) memory only".5 Example: big-int.
r2. Integer literals, including hex, octal and binary literals, give integers.1 int() with no argument returns 0. With a number, it returns x.__int__() or x.__index__(), and for a floating-point number it "truncates towards zero".3 Example: big-int.
r3. int() with a string, or with a base, reads an integer in that base. The string can have a sign, leading zeros, whitespace around it and single underscores between digits. The allowed bases are 0 and 2 to 36. Base 0 reads the string like an integer literal in code, so the prefix 0b, 0o or 0x selects the base, and leading zeros are not allowed.3 Example: not-an-integer-string.
r4. The bitwise operations are "calculated as though carried out in two's complement with an infinite number of sign bits". A negative shift count raises ValueError. A left shift by n bits equals a multiplication by pow(2, n), and a right shift by n bits equals a floor division by pow(2, n).1 Example: bitwise-negative.
r5. int implements the numbers.Integral abstract base class and adds methods.2 bit_length() returns "the number of bits necessary to represent an integer in binary, excluding the sign and leading zeros". bit_count() returns the number of ones in the binary form of the absolute value. to_bytes() and from_bytes() convert between an integer and an array of bytes, with a byte order and an optional two's complement sign.2 Example: int-methods.
r6. CPython limits conversions between int and str in decimal and other bases that are not a power of two, to "mitigate denial of service attacks". The default limit is 4300 digits, and a longer conversion raises ValueError. Hexadecimal, octal and binary conversions are unlimited, and sys.set_int_max_str_digits() changes the limit.4 Example: str-limit.
Examples
bit_length(), bit_count() and the byte conversions (rule r5). The examples come from Built-in Types.
n = -37
print(bin(n), n.bit_length(), (19).bit_count())
print((1024).to_bytes(2, byteorder="big"))
print(int.from_bytes(b"\xfc\x00", byteorder="big", signed=True))
-0b100101 6 3
b'\x04\x00'
-1024
A negative number acts as if it had an infinite row of 1 bits on the left (rule r4).
print(-1 & 0xFF, -8 >> 1, ~5, 1 << 70)
try:
1 << -1
except ValueError:
print("negative shift: ValueError")
255 -4 -6 1180591620717411303424
negative shift: ValueError
A product of two 4300-digit numbers can be computed, but str() refuses to convert it to decimal. hex() still works (rule r6).
import sys
i = int("2" * 4300)
print(len(str(i)), sys.int_info.default_max_str_digits)
square = i * i
try:
str(square)
except ValueError:
print("str(square): ValueError")
print(len(hex(square)))
4300 4300
str(square): ValueError
7144
Pitfalls
int() truncates, it does not floor
int(-3.99) is -3, because int() truncates toward zero (rule r2). Use math.floor() to round down.
This code runs but gives a wrong answer on purpose.
import math
print(int(-3.99), math.floor(-3.99), int(3.99), math.floor(3.99))
-3 -4 3 3
int("3.5") is an error
A string for int() must hold an integer in the given base (rule r3). Convert with float() first, or use round().
This code raises ValueError, on purpose.
print(int("3.5"))
to_bytes() with too few bytes
The default length is one byte, and a value that does not fit raises OverflowError. A negative value with signed=False raises OverflowError too.2
This code raises OverflowError, on purpose.
print((256).to_bytes())
Comparison
Go has integer types of fixed sizes, from int8 and uint8 to int64 and uint64, and an n-bit integer is "represented using two's complement arithmetic". Unsigned arithmetic wraps around, and signed overflow gives a defined value: "Overflow does not cause a run-time panic."6 A Python int has no size, so 2 ** 100 is exact and no operation wraps around (rule r1).
References
- Built-in Types, Python 3.15 documentation, Numeric Types - int, float, complex; Bitwise Operations on Integer Types
- Built-in Types, Python 3.15 documentation, Additional Methods on Integer Types
- Built-in Functions, Python 3.15 documentation, int()
- Built-in Types, Python 3.15 documentation, Integer string conversion length limitation
- The Python Language Reference, Python 3.15, 3.2.4.1. numbers.Integral, Integers (int)
- The Go Programming Language Specification, Numeric types; Integer overflow
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag N4DB6C Edit this page History Report an error Markdown