Home/Blog/Python Number Types | Int Float Complex Guide
Python

Python Number Types | Int Float Complex Guide

Master Python’s numeric data types including integers, floats, and complex numbers with practical examples and conversion techniques.

Python Number Types | Int Float Complex Guide

Python Number Types Overview

Python provides four main types of numbers for different mathematical operations and programming needs:

  • Integers (int) – Whole numbers without decimal points
  • Long integers – Extended range integers (Python 2 only)
  • Floating-point numbers (float) – Numbers with decimal points
  • Complex numbers (complex) – Numbers with real and imaginary parts

Python 3 Update: Long integers have been unified with regular integers in Python 3, eliminating the maximum value limitation for integer types.

Working with Integers

Integers are the most basic numeric type in Python, representing whole numbers without decimal points. They can be positive, negative, or zero, making them perfect for counting, indexing, and basic mathematical operations.

Integer Limits and CPU Architecture

In Python 2, integers had a maximum value based on your system’s architecture:

  • 64-bit systems: Maximum value of 9,223,372,036,854,775,807 (2^63)
  • 32-bit systems: Maximum value of 2,147,483,647 (2^31)
  • Python 3: No maximum limit for integers
# Python 2 maximum integer check
import sys
print(sys.maxint)  # Output: 9223372036854775807

Converting to Integers

Python’s int() function converts other data types to integers, truncating decimal values:

# Convert float to integer
x = 10.7
print(x)        # Output: 10.7
x = int(x)
print(x)        # Output: 10

# Convert string to integer
age = "25"
age = int(age)
print(age)      # Output: 25

Floating-Point Numbers

Floating-point numbers (floats) represent real numbers with decimal points. They’re essential for mathematical calculations that require precision beyond whole numbers, such as financial calculations, scientific computations, and geometric operations.

Creating and Converting Floats

Python automatically recognizes numbers with decimal points as floats. You can also explicitly convert other types using the float() function:

# Declare a float directly
price = 19.99
print(price)    # Output: 19.99

# Convert integer to float
x = 10
print(x)        # Output: 10
x = float(x)
print(x)        # Output: 10.0

# Scientific notation
large_number = 1.5e6  # 1,500,000
print(large_number)   # Output: 1500000.0

Precision Warning: Floating-point arithmetic can introduce small rounding errors. For financial calculations requiring exact precision, consider using Python’s decimal module.

Complex Numbers in Python

Complex numbers consist of real and imaginary parts, expressed in the form a + bj where ‘a’ is the real part and ‘b’ is the imaginary part. They’re commonly used in advanced mathematics, signal processing, and electrical engineering calculations.

Creating Complex Numbers

Python provides multiple ways to create and work with complex numbers:

# Method 1: Direct notation
z1 = 3 + 4j
print(z1)       # Output: (3+4j)

# Method 2: Using complex() function with one argument
x = 10
z2 = complex(x)
print(z2)       # Output: (10+0j)

# Method 3: Using complex() function with two arguments
z3 = complex(5, 7)
print(z3)       # Output: (5+7j)

# Accessing real and imaginary parts
print(z3.real)  # Output: 5.0
print(z3.imag)  # Output: 7.0

Complex Number Operations

# Basic operations with complex numbers
z1 = 3 + 4j
z2 = 1 + 2j

# Addition
result = z1 + z2
print(result)   # Output: (4+6j)

# Multiplication
result = z1 * z2
print(result)   # Output: (-5+10j)

# Conjugate
print(z1.conjugate())  # Output: (3-4j)

Type Conversion Best Practices

Understanding when and how to convert between number types is crucial for efficient Python programming. Here are the key conversion functions and their behaviors:

Function Purpose Example Result
int() Convert to integer int(3.8) 3
float() Convert to float float(5) 5.0
complex() Convert to complex complex(2, 3) (2+3j)
abs() Absolute value abs(-5) 5
round() Round to nearest round(3.7) 4
# Practical type conversion examples
price = "29.99"
quantity = "5"

# Convert string to appropriate numeric types
price_float = float(price)
quantity_int = int(quantity)

total = price_float * quantity_int
print(f"Total: ${total}")  # Output: Total: $149.95

# Type checking
print(type(price_float))   # Output: <class 'float'>
print(type(quantity_int))  # Output: <class 'int'>

Best Practice: Always validate user input before type conversion to prevent ValueError exceptions. Use try-except blocks for robust error handling.

Frequently Asked Questions

Find answers to common questions

Never use float for money—use Decimal every time. Float causes rounding errors: 0.1 + 0.2 = 0.30000000000000004 in Python. For financial apps (invoicing, accounting, e-commerce): use decimal.Decimal which guarantees exact decimal arithmetic. Example: Decimal('10.00') * Decimal('0.075') = exactly Decimal('0.750'), float gives approximations. Performance hit: Decimal is 10-100x slower than float, but for money it's non-negotiable. Use float only for scientific calculations where small rounding errors don't matter (physics simulations, ML training). For percentages in financial contexts: still use Decimal. Import: from decimal import Decimal, then Decimal('value') not Decimal(float) to avoid float contamination.

Automate Your IT Operations

Leverage automation to improve efficiency, reduce errors, and free up your team for strategic work.