Course

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:

python
# 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.

python
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.

python
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:

python
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.

python
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.

python
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:

python
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.

python
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.

python
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.

python
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.

python
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.

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