Python Numbers
Numeric Types Overview
Python has three core numeric types:
- **Integers (`int`)**: Whole numbers (positive, negative, or zero)
- **Floating-point (`float`)**: Decimal numbers with fractional parts
- **Complex (`complex`)**: Numbers with real and imaginary components
Type | Example | Characteristics |
---|---|---|
int | 42, -7, 0 | Unlimited precision |
float | 3.14, -0.001, 2.5e5 | 64-bit double precision |
complex | 3+4j, -2j | Stores real and imaginary parts |
Integer Operations
Example
# Basic arithmetic
a = 10 + 3 # 13
b = 10 - 3 # 7
c = 10 * 3 # 30
d = 10 // 3 # Floor division → 3
e = 10 % 3 # Modulus → 1
f = 10 ** 3 # Exponentiation → 1000
# Bitwise operations
g = 0b1010 & 0b1100 # AND → 8
h = 0b1010 | 0b1100 # OR → 14
Output
# Results shown in comments
ℹ️ Note: Python 3 integers can grow arbitrarily large, limited only by memory.
Float Precision
Floating-point numbers follow the IEEE 754 standard. They can represent very large and very small numbers but with rounding limitations.
⚠️ Warning: Avoid floats in financial applications—use the `decimal` module instead.
Example
# Precision examples
x = 0.1 + 0.2
print(x) # 0.30000000000000004
print(1e300) # 1e+300
print(1e300 * 1e300) # inf (overflow)
# Exact decimal arithmetic with Decimalrom decimal import Decimal
y = Decimal('0.1') + Decimal('0.2')
print(y) # 0.3
Output
0.30000000000000004 1e+300 inf 0.3
Complex Numbers
Example
# Complex number operations
z1 = 3 + 4j
z2 = 2 - 1j
print(z1 + z2) # (5+3j)
print(z1 * z2) # (10+5j)
print(abs(z1)) # Magnitude → 5.0
print(z1.conjugate()) # (3-4j)
Output
(5+3j) (10+5j) 5.0 (3-4j)
ℹ️ Note: Access real and imaginary parts with `.real` and `.imag`.
Type Conversion
You can explicitly convert between numeric types and parse strings into numbers.
⚠️ Warning: Converting float to int truncates the decimal part, it does not round.
Example
# Safe conversions
x = 42
float_x = float(x) # 42.0
complex_x = complex(x) # (42+0j)
# Truncating conversion
y = 3.99
int_y = int(y) # 3
# String parsing
z = int("255") # 255
hex_z = int("FF", 16) # 255 from hexadecimal
Number Systems
Example
# Base conversions
print(bin(42)) # '0b101010'
print(hex(255)) # '0xff'
print(int('1010', 2)) # 10
Output
0b101010 0xff 10
Base | Prefix | Example |
---|---|---|
Binary (2) | 0b | 0b1010 → 10 |
Octal (8) | 0o | 0o755 → 493 |
Decimal (10) | — | 42 → 42 |
Hexadecimal (16) | 0x | 0xFF → 255 |
Math Module Functions
The `math` module provides common mathematical constants and functions.
Example
import math
# Constants
print(math.pi) # 3.141592...
print(math.e) # 2.718281...
# Functions
print(math.sqrt(25)) # 5.0
print(math.factorial(5)) # 120
print(math.gcd(48, 18)) # 6
print(math.ceil(3.2)) # 4
print(math.floor(3.8)) # 3
Output
3.141592653589793 2.718281828459045 5.0 120 6 4 3
Practical Applications
- Finance (use `decimal` for accuracy)
- Scientific computing (`numpy` for arrays and matrices)
- Game development (coordinates, vectors, physics)
- Cryptography (large integer arithmetic)
Example
# Compound interest example
principal = 1000
rate = 0.05
years = 5
amount = principal * (1 + rate) ** years
print(f"${amount:.2f}")
Output
$1276.28