Python Numeric Types: Int, Float, and Complex

Python supports three distinct numeric types to handle different mathematical requirements. Unlike many other languages where you must manage memory sizes (like short, long, or double), Python abstracts these details away, providing a seamless experience for both simple arithmetic and scientific computing.

Numbers in Python are immutable objects, meaning when you perform an operation on a number, a new object is created in memory to store the result.

1. Integers (int)

In Python 3, the int type is a whole number of unlimited precision. This is a unique feature compared to languages like C++ or Java, where integers are usually capped at 64 bits.

  • Positive/Negative: Can be any whole number (e.g., 5, -500).
  • No Size Limit: You can calculate $2^{1000}$ without an overflow error, provided your computer has enough RAM.
  • Bases: Integers can be represented in Binary (0b), Octal (0o), or Hexadecimal (0x).
Python
Integer examples and base representation
a = 12345678901234567890  # Large integer
b = 0b1010                # Binary (10 in decimal)
c = 0xFF                  # Hex (255 in decimal)

print(type(a))            # <class 'int'>
print(b + 1)              # 11

2. Floating Point Numbers (float)

The float type represents real numbers and is implemented using double precision (64-bit) binary format (IEEE 754). This allows for about 15-17 significant decimal digits.

Note: Because floats are stored in binary, certain decimal fractions cannot be represented exactly (e.g., $0.1 + 0.2$ results in $0.30000000000000004$).

Python
Float and Scientific Notation
pi = 3.14159
scientific = 1.2e-3    # 0.0012

print(type(pi))        # <class 'float'>
print(0.1 + 0.2 == 0.3) # False (due to precision issues)

3. Complex Numbers (complex)

Python is one of the few mainstream languages with built-in support for complex numbers. They are written in the form $a + bj$, where $a$ is the real part and $b$ is the imaginary part. Note that Python uses j instead of i.

Python
Working with complex numbers
z = 3 + 5j

print(z.real)    # 3.0
print(z.imag)    # 5.0
print(z.conjugate()) # (3-5j)

# Multiplication of complex numbers
print((1+2j) * (1-2j)) # (5+0j)

Quick Comparison of Numeric Types

Featureintfloatcomplex
PrecisionArbitrary (Unlimited)64-bit (Limited)2x 64-bit floats
Example4242.042 + 0j
MutabilityImmutableImmutableImmutable
Scientific NotationNoYes (1e10)Yes (1e10j)

4. Type Conversion and Coercion

Python automatically coerces types during arithmetic operations (Implicit Conversion). It promotes the 'narrower' type to a 'wider' type to prevent data loss.

  • int + float results in float
  • float + complex results in complex
Python
Explicit type casting
# Convert float to int (truncates towards zero)
print(int(3.9))   # 3

# Convert int to complex
print(complex(10)) # (10+0j)

# Convert string to float
print(float("10.5")) # 10.5

Conclusion

Understanding int, float, and complex types is vital for any Python developer. Use int for counting and discrete values, float for measurements and calculations involving decimals, and complex for specialized engineering or mathematical simulations.