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 and are typically used for counting, indexing, or when you need to perform arithmetic operations that don’t require fractions:
>>> number = 42
>>> type(number)
<class 'int'>
>>> negative = -5
>>> zero = 0
int
Constructors
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:
>>> int()
0
>>> 0
0
Creating integer instances using literals in various bases:
>>> decimal = 42
>>> binary = 0b101010
>>> octal = 0o52
>>> hexadecimal = 0x2A
>>> decimal, binary, octal, hexadecimal
(42, 42, 42, 42)
Creating an integer using the class constructor:
>>> 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 number of ones in the binary representation of the integer. |
.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() |
Returns True (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:
>>> 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 crucial for converting binary strings into decimal numbers, allowing you to perform further calculations or analyses on the data.
Related Resources
Tutorial
Numbers in Python
In this tutorial, you'll learn about numbers and basic math in Python. You'll explore integer, floating-point numbers, and complex numbers and see how perform calculations using Python's arithmetic operators, math functions, and number methods.
For additional information on related topics, take a look at the following resources:
- Basic Data Types in Python: A Quick Exploration (Tutorial)
- How to Read Python Input as Integers (Tutorial)
- Python's Built-in Functions: A Complete Exploration (Tutorial)
- Exploring Basic Data Types in Python (Course)
- Basic Data Types in Python: A Quick Exploration (Quiz)
- Python's Built-in Functions: A Complete Exploration (Quiz)