Python int (Integer) Data Type

This page is a summary of Python int (integer) data type – its basic characteristics, how to create an int, how to convert other data types to int, basic arithmetic and comparison operations, and typical use cases.

Basic characteristics

Python int data type represents integers = whole numbers = numbers without decimal point. This is the main difference from Python float data type, which is a floating point number (number with decimal point).

An int can be positive, negative, or zero.

The int data type as well as the corresponding int() function are built-in. You don't need to import anything to use them.

How to create an int

The simplest way to create an int variable is by assigning a whole number to it, using the = assignment operator.

a = 123
b = -456
c = 0

Converting other data types to int

Alternatively, an int can be created by converting another type variable to it, using the int() function.

str to int

For example, this converts the string '123' to the int 123:

a = int('123')

float to int

When a float is converted to int, it loses everything after the decimal point:

>>> int(123.456)
123
>>> int(-123.456)
-123

As a result, a positive float is rounded down to the nearest smaller integer (closer to zero), while a negative float is rounded up to the nearest higher integer (closer to zero).

Arithmetic operations with ints

Python supports all common arithmetic operations using the usual symbols:

  • Addition: a + b
  • Subtraction: a - b
  • Multiplication: a * b
  • Division: a / b
  • Modulo (remainder): a % b
  • Exponentiation: a ** b
>>> 5 + 2  # 5 plus 2
7
>>> 5 - 2  # 5 minus 2
3
>>> 5 * 2  # 5 times 2
10
>>> 5 / 2  # 5 divided by 2
2.5
>>> 5 % 2  # remainder of 5 divided by 2
1
>>> 5 ** 2  # 5 to the power of 2 (5 squared)
25

All these operations return another int, except division which returns a float, even when the result would be a whole number mathematically. For example:

>>> 10 / 5
2.0
>>> type(10 / 5)
<class 'float'>

To avoid this an get an integer, use the integer division operator // (double slash):

>>> 10 // 5
2
>>> type( 10 // 5)
<class 'int'>

Comparison operators

Python supports all the common comparison operators between integers. They return a bool (True/False):

>>> 12 > 5  # 12 is greater than 5
True
>>> 12 >= 5  # 12 is greater than or equal to 5
True
>>> 12 < 5  # 12 is smaller than 5
False
>>> 12 <= 5  # 12 is smaller than or equal to 5
False
>>> 5 <= 5
True
>>> 12 == 5  # 12 equals 5
False
>>> 12 == 12
True
>>> 12 != 5  # 12 does not equal 5
True
>>> 12 != 12
False

Comparison operators also work between ints and other numeric types, such as floats:

>>> 12 > 5.75
True
>>> 12 <= 5.75
False
>>> 12 == 12.0
True

However, comparison operators do not work between ints and strings, even when the string represents a numeric value. We get a TypeError:

>>> 12 > '5'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '>' not supported between instances of 'int' and 'str'

Boolean evaluation of ints

When an int is used in the boolean context or evaluated using the bool() function:

>>> bool(0)
False
>>> bool(123)
True
>>> bool(-123)
True

Typical use cases

As one of the most basic data types, int has a wide variety of uses in Python. The following are just of few common examples.

Discrete numeric variables, such as number of persons, number of website visitors, number of items sold or items in stock, or number of seats in a concert hall.

Moreover, integers are often used to represent even variables which are not discrete in the real world, but the particular use case does not require decimal point precision. For example, age or financial amounts.

Date and time. Although there are modules such as datetime dedicated to working with time and date, at the low level they work with integers – years, months, days, hours, minutes, seconds, microseconds, or weekdays.

Primary key in databases and big data applications. Because of its simplicity and performance benefits, int is typically used as primary key and unique identifier of rows in databases and big datasets.

Indexing and slicing. An int can represent a character position in a string or an item position in a list, tuple, or another sequence:

>>> s = 'PyTut'
>>> s[1]
'y'
>>> colors = ['red', 'green', 'blue']
>>> colors[2]
'blue'

Counter in loops. The typical use case is an int as argument to range() in a for-loop:

for i in range(20):
    # do something 20 times

Coordinates in game development and other graphics. Integers represent x and y positions of individual pixels.

Error handling. Integers are typically used as error codes to identify what went wrong in a script execution.

Official documentation

Numeric data types (int, float, complex):
https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex

The int() function:
https://docs.python.org/3/library/functions.html#int

By remaining on this website or using its content, you confirm that you have read and agree with the Terms of Use Agreement.

We are not liable for any damages resulting from using this website. Any information may be inaccurate or incomplete. See full Limitation of Liability.

Content may include affiliate links, which means we may earn commission if you buy on the linked website. See full Affiliate and Referral Disclosure.

We use cookies and similar technology to improve user experience and analyze traffic. See full Cookie Policy.

See also Privacy Policy on how we collect and handle user data.

© 2024 PyTut