Python > Core Python Basics > Fundamental Data Types > Integers (int)

Integer Declaration and Basic Operations

This snippet demonstrates how to declare integers in Python and perform basic arithmetic operations. Integers are a fundamental data type representing whole numbers.

Integer Declaration

This code snippet declares an integer variable named `my_integer` and assigns it the value 10. The `print()` function displays the value of the variable, and `type()` function confirms that it is an integer.

my_integer = 10
print(my_integer)
print(type(my_integer))

Basic Arithmetic Operations

This example shows basic arithmetic operations on integers. Note that standard division `/` returns a floating-point number, while floor division `//` returns an integer. The modulo operator `%` returns the remainder of a division.

a = 5
b = 3

addition = a + b
subtraction = a - b
multiplication = a * b
division = a / b  # Returns a float
floor_division = a // b # Returns an integer (floor)
exponentiation = a ** b
modulo = a % b

print(f"Addition: {addition}")
print(f"Subtraction: {subtraction}")
print(f"Multiplication: {multiplication}")
print(f"Division: {division}")
print(f"Floor Division: {floor_division}")
print(f"Exponentiation: {exponentiation}")
print(f"Modulo: {modulo}")

Concepts Behind the Snippet

Integers are immutable, meaning their value cannot be changed after creation. When you perform operations, you create new integer objects. Python automatically manages memory allocation for integers. The range of integers is limited only by the available memory (for Python 3).

Real-Life Use Case

Integers are used extensively in counting, indexing arrays, performing calculations in scientific simulations, and representing quantities in financial applications. For example, tracking the number of users on a website, calculating the total cost of items in a shopping cart, or storing the number of iterations in a loop.

Best Practices

Use descriptive variable names to improve code readability. Consider using type hints (e.g., `a: int = 5`) to enhance code clarity and maintainability. Use floor division `//` when you need an integer result from a division operation and avoid potential floating-point inaccuracies.

Interview Tip

Be prepared to discuss the difference between integer division and floor division, the immutability of integers, and the memory management of integers in Python. Also, know the range of integers that Python can handle.

When to Use Them

Use integers when you need to represent whole numbers without fractional components. They are ideal for representing counts, quantities, indices, and other discrete values. Avoid using integers when you need high precision decimal representations; use the `decimal` module instead.

Memory Footprint

Python integers are dynamically sized. The memory usage depends on the magnitude of the number. Larger numbers require more memory to store. Small integers (typically -5 to 256) are often interned by Python, meaning they are pre-allocated and reused, saving memory.

FAQ

  • What is the difference between `/` and `//` division?

    The `/` operator performs standard division and returns a floating-point number. The `//` operator performs floor division and returns an integer, truncating any fractional part.
  • Are Python integers mutable?

    No, Python integers are immutable. When you perform an operation that appears to modify an integer, you are actually creating a new integer object.
  • What is integer interning in Python?

    Python interns small integers (typically -5 to 256) for optimization. This means that these integers are pre-allocated and reused whenever they are needed, saving memory and improving performance.