Glossary
HTML
CSS
JavaScript
Python

PYTHON

Python Integer: Syntax, Usage, and Examples

Integer, or int, is a data type in Python that represents whole numbers without a decimal point.

How to Use Python Integers

To create and use an integer in Python, simply assign a whole number to a variable:

# Assigning integer values
count = 25

When to Use Python Integers

Integer numbers are essential for working with whole numbers in Python code. From simple arithmetic operations to controlling loops, indexing, and more, integers make up virtually every Python program.

Examples of Python Integers

Representing Data

Integers are ideal for representing data where fractional parts are not applicable. As examples, consider age, the number of people, or the number of items.

age = 20
number_of_students = 45
pages_in_a_book = 300

Mathematical Operations

Integers are essential for performing mathematical calculations, particularly where floating-point arithmetic could lead to errors.

x = 5
y = 3
result = x * y  # Result is 15, exact and integer

Counting and Iterations

Integers are commonplace in loops for counting and iterating through collections:

for i in range(5):
    print(i)  # Outputs: 0, 1, 2, 3, 4

Indexing and Accessing Data

Arrays, lists, and other collections use integers to represent the position, or index, of their elements.

numbers = [10, 20, 30, 40]
print(numbers[2])  # Outputs: 30

Learn More About Python Integers

Converting String to Integer in Python

You can use the int() function to convert a Python string to an integer. Once converted, the integer-type number is safe to use for calculations. String conversions are often necessary when processing user input or receiving data from external sources.

number = int("10")  # Converts string to integer
sum = number + 5
print(sum)  # Outputs: 15

Converting Integer to String in Python

Converting integers to strings is common in Python, especially when you need to concatenate numbers with text for display:

age = 25
text = "Your age is " + str(age)
print(text)  # Outputs: Your age is 25

Formatting Integers in Python

Especially when displaying large integers, using separators for thousands might make an integer easier to read. Using f-string formatting, you can control how you want an integer to display as a string.

budget = 1234567
print(f"{budget:,}")  # Output: '1,234,567' formats with commas

Generating Random Integers in Python

With the random module, Python can generate random numbers. The randint() function in Python can provide a useful random-number generator for games, simulations, and testing purposes.

import random

# Generate a random integer within a range
rand_int = random.randint(1, 100)
print(rand_int)

Python Integer Division

Dividing an integer by another integer normally produces a float.

result = 10 / 3
print(result)  # Outputs: 3.3333333333333335

Integer division, performed using //, discards the remainder and returns an integer result. This is particularly useful in scenarios where you need an exact division, such as distributing items evenly without considering remainders.

result = 10 // 3
print(result)  # Outputs: 3

Max Integer in Python

Many programming languages have a maximum integer value. Python integers, however, are of arbitrary precision and can grow as large as memory allows.

huge_number = 12345678901234567890
print(huge_number + 1)  # Outputs: 12345678901234567891

Learn to Code in Python for Free
Start learning now
To advance beyond this tutorial and learn Python by doing, try the interactive experience of Mimo. Whether you're starting from scratch or brushing up your coding skills, Mimo helps you take your coding journey above and beyond.

Sign up or download Mimo from the App Store or Google Play to enhance your programming skills and prepare for a career in tech.

You can code, too.

© 2023 Mimo GmbH