Visitar URL original
int | Python’s Built-in Data Types – Real Python
Skip to content

int

The built-in int data type represents integer numbers, which are whole numbers without any decimal places. Integers can be positive, negative, or zero. They’re typically used for counting, indexing, or when you need to perform arithmetic operations that don’t require fractions:

Language: Python
>>> number = 42
>>> type(number)
<class 'int'>
>>> negative = -5
>>> zero = 0

int Constructors

Language: Python Syntax
int(number=0, /)
int(string, /, base=10)

Arguments

Argument Description Default Value
number A numeric type or object that can be converted to an integer. 0
string A string representing an integer in a given base. Required
base The base of the number in string. Only valid if the first argument is a string. Valid bases are 0 and 2–36. 10

Return Value

  • Returns a Python int object

int Examples

Creating a zero integer by calling int() without arguments or a literal:

Language: Python
>>> int()
0
>>> 0
0

Creating integer instances using literals in various bases:

Language: Python
>>> decimal = 42
>>> binary = 0b101010
>>> octal = 0o52
>>> hexadecimal = 0x2A

>>> decimal, binary, octal, hexadecimal
(42, 42, 42, 42)

Creating an integer using the class constructor:

Language: Python
>>> int("101010", 2)
42

int Methods

Method Description
.as_integer_ratio() Returns a pair of integers representing the ratio of the integer and 1.
.bit_count() Returns the population count, which is the number of ones in the binary representation of the integer’s absolute value.
.bit_length() Returns the number of bits required to represent the integer in binary.
.from_bytes() Returns the integer represented by an array of bytes.
.to_bytes() Returns an array of bytes representing the integer.
.is_integer() Always returns True and exists mainly for compatibility with floating-point numbers.

int Common Use Cases

The most common use cases for the int data type include:

  • Counting items or occurrences
  • Indexing elements in a list or other collections
  • Performing arithmetic operations
  • Representing numerical data in various bases

int Real-World Example

Consider an example where you need to convert a list of strings of binary numbers to their decimal integer equivalents. This is a common requirement when dealing with data encoded in binary format:

Language: Python
>>> binary_strings = ["1101", "1010", "1111"]
>>> decimal_numbers = [int(b, 2) for b in binary_strings]
>>> decimal_numbers
[13, 10, 15]

In this example, the int data type is essential for converting binary strings into decimal numbers, allowing you to perform further calculations or analyses on the data.

How to Read Python Input as Integers

Tutorial

How to Read Python Input as Integers

In this tutorial, you'll learn how to use Python to get integer input from the user while handling any errors resulting from non-numeric input. This will involve coding your own reusable function built around input().

basics python stdlib

For additional information on related topics, take a look at the following resources:

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Oct. 8, 2026 • Reviewed by Brenda Weleschuk and Dan Bader