On this page
int
Prev: input | Up: __builtins__ | Next: isinstance
Rel: bool, float, complex, str, bytes
Ref: docs.python.org — int
Tags: #public #python3 #builtins
Import & Availability
# Built-in (available globally without import)
from builtins import int
Overview & Signature
int([x]) -> integer
int(x, base=10) -> integer Convert a number or string to an integer, or return 0 if no arguments are given. If x is a number, return x.int(). For floating-point
Common Methods & Attributes
int.as_integer_ratio(self, /)— Return a pair of integers, whose ratio is equal to the original int.int.bit_count(self, /)— Number of ones in the binary representation of the absolute value of self.int.bit_length(self, /)— Number of bits necessary to represent self in binary.int.conjugate(self, /)— Returns self, the complex conjugate of any int.int.denominator(...)— the denominator of a rational number in lowest termsint.from_bytes(bytes, byteorder='big', *, signed=False)— Return the integer represented by the given array of bytes.int.imag(...)— the imaginary part of a complex numberint.is_integer(self, /)— Returns True. Exists for duck type compatibility with float.is_integer.int.numerator(...)— the numerator of a rational number in lowest termsint.real(...)— the real part of a complex numberint.to_bytes(self, /, length=1, byteorder='big', *, signed=False)— Return an array of bytes representing an integer.
Usage
assert int("42") == 42
assert int("ff", 16) == 255
assert int("0b1010", 0) == 10 # base=0 auto-detects 0b/0o/0x prefixes
assert int(-3.9) == -3 # truncates toward zero
Gotchas
- Truncates toward zero (unlike
math.floor):int(-3.9)is-3, whereasmath.floor(-3.9)is-4. - Arbitrary precision: Python ints never overflow 64 bits (only string conversion is capped by
sys.get_int_max_str_digits()).
REPL Inspect
help(int)
Linked from
- tuts/python3/__builtins__
- tuts/python3/__builtins__/__builtins__
- tuts/python3/__builtins__/bool
- tuts/python3/__builtins__/bytearray
- tuts/python3/__builtins__/bytes
- tuts/python3/__builtins__/complex
- tuts/python3/__builtins__/dict
- tuts/python3/__builtins__/float
- tuts/python3/__builtins__/frozenset
- tuts/python3/__builtins__/input
- tuts/python3/__builtins__/isinstance
- tuts/python3/__builtins__/list
- tuts/python3/__builtins__/memoryview
- tuts/python3/__builtins__/object
- tuts/python3/__builtins__/range
- tuts/python3/__builtins__/set
- tuts/python3/__builtins__/slice
- tuts/python3/__builtins__/str
- tuts/python3/__builtins__/tuple
- tuts/python3/__builtins__/type
- tuts/python3/tutorials/core_fundamentals/primitive_data_types
Related notes
- tuts/python3/__builtins__/__builtins__ Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/bool Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/bytes Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/complex Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/float Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/input Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/isinstance Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/str Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__ links to this page; shared tags: #builtins
- tuts/python3/__builtins__/bytearray links to this page; shared tags: #builtins