Python Basics — Numbers
Numbers
Python has three numeric types: integers, floating-point numbers, and complex numbers. Most of the time, you’ll work with integers and floats.
Integers (int)
Integers are whole numbers — positive, negative, or zero — with no decimal point.
age = 25
temperature = -10
count = 0
big_number = 1_000_000 # Underscores for readability
print(type(age)) # <class 'int'>
Python integers have unlimited precision — they can be as large as your memory allows:
huge = 10 ** 100 # 10 raised to the power of 100
print(huge)
# 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
print(type(huge)) # <class 'int'>
Integer Bases
decimal = 42 # Base 10 (default)
binary = 0b101010 # Base 2 (prefix 0b)
octal = 0o52 # Base 8 (prefix 0o)
hexadecimal = 0x2A # Base 16 (prefix 0x)
print(decimal, binary, octal, hexadecimal)
# 42 42 42 42 — all represent the same number!
Floating-Point Numbers (float)
Floats are numbers with a decimal point:
price = 19.99
pi = 3.14159
negative = -0.5
scientific = 2.5e3 # 2.5 × 10³ = 2500.0
print(type(price)) # <class 'float'>
Float Precision Warning
Floats have limited precision due to how computers store decimal numbers in binary:
print(0.1 + 0.2) # 0.30000000000000004 (not 0.3!)
print(0.1 + 0.2 == 0.3) # False
How to handle this: Use round() or the decimal module for precise calculations:
# Using round()
result = round(0.1 + 0.2, 1)
print(result) # 0.3
print(result == 0.3) # True
# Using decimal module for financial calculations
from decimal import Decimal
a = Decimal("0.1")
b = Decimal("0.2")
print(a + b) # 0.3 (exact!)
Complex Numbers (complex)
Complex numbers have a real part and an imaginary part:
z = 3 + 4j
print(z) # (3+4j)
print(type(z)) # <class 'complex'>
print(z.real) # 3.0
print(z.imag) # 4.0
You probably won’t use complex numbers unless you’re working in scientific computing or engineering.
Numeric Type Comparison
| Type | Example | Use Case |
|---|---|---|
int |
42, -7, 0 |
Counting, indexing, IDs |
float |
3.14, -0.5, 2.5e3 |
Measurements, calculations |
complex |
3+4j |
Scientific computing |
The math Module
Python’s built-in math module provides mathematical functions:
import math
print(math.sqrt(16)) # 4.0 — square root
print(math.ceil(4.2)) # 5 — round up
print(math.floor(4.8)) # 4 — round down
print(math.pow(2, 10)) # 1024.0 — power
print(math.pi) # 3.141592653589793
print(math.e) # 2.718281828459045
print(math.factorial(5)) # 120 — 5! = 5×4×3×2×1
print(math.log(100, 10)) # 2.0 — log base 10 of 100
print(math.gcd(12, 18)) # 6 — greatest common divisor
The random Module
Generate random numbers with the random module:
import random
print(random.randint(1, 10)) # Random integer from 1 to 10
print(random.random()) # Random float from 0.0 to 1.0
print(random.uniform(1.0, 5.0)) # Random float from 1.0 to 5.0
print(random.choice([1, 2, 3])) # Random element from a list
# Shuffle a list in place
cards = [1, 2, 3, 4, 5]
random.shuffle(cards)
print(cards) # e.g., [3, 1, 5, 2, 4]
Built-in Numeric Functions
| Function | Description | Example |
|---|---|---|
abs(x) |
Absolute value | abs(-5) → 5 |
round(x, n) |
Round to n decimals | round(3.14159, 2) → 3.14 |
pow(x, y) |
x to the power of y | pow(2, 3) → 8 |
divmod(x, y) |
Quotient and remainder | divmod(17, 5) → (3, 2) |
min(...) |
Minimum value | min(3, 1, 4) → 1 |
max(...) |
Maximum value | max(3, 1, 4) → 4 |
sum(iterable) |
Sum of elements | sum([1, 2, 3]) → 6 |
Number Formatting
price = 1234567.891
# Comma separator
print(f"{price:,.2f}") # 1,234,567.89
# Percentage
rate = 0.856
print(f"{rate:.1%}") # 85.6%
# Padding with zeros
code = 42
print(f"{code:05}") # 00042
# Binary, octal, hex
num = 255
print(f"{num:b}") # 11111111
print(f"{num:o}") # 377
print(f"{num:x}") # ff
print(f"{num:X}") # FF
Common Mistakes
| Mistake | Example | Fix |
|---|---|---|
| Integer division surprise | 7 / 2 → 3.5 (not 3) |
Use // for integer division |
| Float comparison | 0.1 + 0.2 == 0.3 → False |
Use round() or math.isclose() |
| Division by zero | 10 / 0 → ZeroDivisionError |
Check divisor before dividing |
| String + number | "Age: " + 25 → TypeError |
Use f-string: f"Age: {25}" |
Summary
- Integers (
int) handle whole numbers with unlimited precision - Floats (
float) handle decimal numbers but have precision limitations - Complex (
complex) numbers have real and imaginary parts - Use the math module for mathematical functions (sqrt, ceil, floor)
- Use the random module for generating random numbers
- Format numbers with f-strings for commas, percentages, and padding
- Be careful with float comparisons — use
round()ormath.isclose()
Next up: Strings Basics →