Skip to main content
Updated Feb 26, 2026

Numeric Types: Integers, Floats, and Complex Numbers

What You'll Learn

  • Whole numbers (int)
  • Decimal numbers (float)
  • Complex numbers (advanced)
  • When to use each type
  • Avoiding common mistakes

You walk into a store. You see two prices:

  • Age: 25 years old
  • Item Price: $25.99

Why is one written 25 and the other 25.99? They're both numbers, but they're different kinds of numbers. Age is a whole number—you can't be 25.5 years old. Price is a decimal number—you need cents. Python has different types for these different kinds of numbers.

In this lesson, you'll learn when to use each numeric type and why it matters for your code.


WHAT Is an Integer?

A integer (or int) is a whole number. No decimal points. No fractions.

Characteristics of Integers

  • Whole numbers only: 5, 100, -3, 0
  • Positive, negative, or zero: Can represent direction or absence
  • Unlimited range: Python 3 has no upper limit (unlike older languages)
  • Exact values: 25 is always exactly 25—never 25.0

Real-World Examples

Think about when you naturally count whole numbers:

  • Age: "I'm 25 years old" (not 25.5)
  • Student count: "There are 30 students in class" (not 30.7 students)
  • Array index: "The first item is at position 0"
  • Inventory: "We have 150 boxes in stock"
  • Score: "You earned 95 points"

WHY Use Integers?

Use int when you need exact whole numbers and precision matters. You can't have half a person, half a product, or a fractional index. Integers guarantee no rounding errors.

WHEN to Choose Integer Type

Ask yourself: "Is this something that makes sense as a whole number only?"

  • Counting discrete items → int
  • Measuring continuous quantities (distance, weight) → not int
  • Storing True/False → not int (use bool)
  • Building arrays/lists → int (for indices)

NOW Show Integer Code

Here's how you declare integer variables with type hints:

age: int = 25
student_count: int = 100
list_index: int = 0
temperature: int = -5
inventory: int = 500

Notice: Each variable includes a type hint (: int). This tells Python (and anyone reading your code) that this variable should hold an integer.

Verify with type() (from Lesson 1):

print(type(age))              # <class 'int'>
print(type(student_count)) # <class 'int'>
print(type(-5)) # <class 'int'>

💬 AI Colearning Prompt

"Why does Python differentiate between 25 (int) and 25.0 (float) when they represent the same value? What operations would differ?"

This helps you understand that types aren't just labels—they represent different computational behaviors.


WHAT Is a Float?

A float is a number with a decimal point. It can represent fractions and very large/small numbers.

Characteristics of Floats

  • Decimal points: 3.14, 0.5, -2.75
  • Represent fractions and measurements: Can be 3.7 km, not just 3 km or 4 km
  • Scientific notation: 1.5e-3 means 0.0015 (small) or 2.5e6 means 2,500,000 (large)
  • Approximate values: Due to IEEE 754 floating-point standard (Advanced topic), some floats can't be represented exactly

Real-World Examples

When do you naturally work with decimals?

  • Price: "$19.99" (dollars and cents)
  • Measurement: "3.14 meters of cloth"
  • Percentage: "0.85" (85% as a decimal)
  • Scientific data: "3.14159" (π in calculations)
  • Temperature: "37.5 degrees Celsius"
  • Weight: "2.5 kilograms"

WHY Use Floats?

Use float when precision after the decimal point matters. Prices, measurements, and scientific values need decimal accuracy.

WHEN to Choose Float Type

Ask yourself: "Does this value need a decimal point?"

  • Prices with cents → float
  • Measurements of continuous quantities → float
  • Percentages expressed as decimals → float
  • Scientific/mathematical calculations → float
  • Counts of discrete items → not float (use int)

NOW Show Float Code

Here's how you declare float variables:

price: float = 19.99
pi: float = 3.14159
percentage: float = 0.85
height: float = 1.75
temperature: float = 37.5
epsilon: float = 1e-6

Verify the type with type():

print(type(price))           # Output: <class 'float'>
print(type(3.14)) # Output: <class 'float'>
print(type(1e-6)) # Output: <class 'float'>

🎓 Expert Insight

In AI-native development, you don't memorize floating-point edge cases—you understand WHEN precision matters and ask AI to explain unexpected behavior. Syntax is cheap; recognizing "this calculation needs decimal accuracy" is gold.


Decision Guide: Integer vs. Float

Remember the Type Decision Framework from Lesson 1? Here's how it applies specifically to choosing between int and float:

ScenarioTypeWhy
Age (years)intNo fractional ages exist
Price (dollars and cents)floatNeeds decimal precision
Student countintCan't have 25.5 students
Distance traveled (km)floatCan be 3.7 km, not just 3 or 4
Number of items soldintDiscrete count
Average score (points)floatMay not be whole (87.5 average)
Inventory itemsintCounting whole units
Weight in kilogramsfloatContinuous measurement
Array indexintAlways whole number
Calculation result (ratio)floatMay include decimals

🤝 Practice Exercise

Ask your AI: "Give me 15 real-world scenarios (like 'distance to store', 'number of emails received'). For each, should I use int or float? Explain your reasoning, then I'll classify them."

Expected Outcome: You'll build intuition for when decimals matter. When you disagree with AI, ask why—this reveals edge cases you haven't considered.


WHAT Is Complex? — Advanced Topic

A complex number combines a real part and an imaginary part: a + bj.

Characteristics of Complex Numbers

  • Format: real_part + imaginary_part*j (example: 2+3j)
  • Real part: Normal decimal number
  • Imaginary part: Coefficient of j (the imaginary unit)
  • Specialized use: Scientific computing, engineering, signal processing, physics simulations

Real-World Application (Why It Exists)

Complex numbers aren't just theoretical—they solve real problems:

  • Electrical engineering: AC circuit analysis
  • Physics: Quantum mechanics simulations
  • Signal processing: Audio/image analysis
  • Control systems: Stability analysis

Code Example

z: complex = 2 + 3j
print(type(z)) # Output: <class 'complex'>
print(z.real) # Output: 2.0 (real part)
print(z.imag) # Output: 3.0 (imaginary part)
print(z + (1+2j)) # Output: (3+5j) (complex addition)

The Honest Message for Beginners

Know it exists. You'll rarely need it as a beginner.

Complex numbers are specialized. Unless you're working in physics, engineering, or signal processing, you won't use them in early projects. Come back to this when you need it—and the book will guide you then.


Common Numeric Type Mistakes

Even experienced developers make these mistakes. Here are five to watch for:

Mistake 1: Storing a Decimal in an Integer Variable (Type Loss)

# WRONG: Trying to use int for a decimal value
age: int = 25.5 # Type hint says int, but value is 25.5
# Python allows this but might truncate: age becomes 25

Why it matters: You lose information. The .5 disappears.

Fix: Use float when decimals are involved.

age: float = 25.5  # Correct

Mistake 2: Integer Division Surprise

result: int = 5 / 2
print(result) # You might expect 2, but get 2.5!
print(type(result)) # <class 'float'> — not int!

Why it happens: In Python 3, / always returns a float, even if both numbers are integers.

Fix: Use // for integer division (discards the decimal):

result: int = 5 // 2  # Now result is 2 (exact)
print(type(result)) # <class 'int'>

Mistake 3: Float Precision Issues (Advanced)

result: float = 0.1 + 0.2
print(result) # Output: 0.30000000000000004 (not 0.3!)

Why it happens: IEEE 754 floating-point representation can't store 0.1 exactly. Tiny rounding errors accumulate.

When it matters: Financial calculations (use the Decimal module instead).

For most cases: This is fine. Use round() if you need clean output:

print(round(0.1 + 0.2, 1))  # Output: 0.3

Mistake 4: Using Float for Money (Anti-Pattern)

# DON'T: Using float for financial calculations
price: float = 19.99
# Precision issues make this risky for transactions

Why it matters: Floating-point rounding can cause tiny errors that become big problems at scale.

Fix: For production financial systems, use the Decimal module (covered in later chapters).

from decimal import Decimal
price: Decimal = Decimal("19.99") # Exact representation

Mistake 5: Confusing Type Hints with Type Conversion

# Type hint says what type the variable SHOULD hold
value: int = 5 # ✓ Correct: value is an integer

# Python doesn't enforce type hints at runtime (that's optional linting)
value: int = "hello" # Python allows this, but it's wrong!
print(type(value)) # <class 'str'> — violated the hint

The lesson: Type hints are promises to yourself and other developers, not enforced rules. Tools like mypy check them, but Python runtime doesn't.


Practice Exercises

Exercise 1: Classify 15 Numbers

Classify each value as int, float, or complex. Write your reasoning.

1. 42
2. 3.14
3. -5
4. 0.5
5. 100
6. 2 + 3j
7. -0.001
8. 1000000
9. 0.0
10. 1e10
11. -3 - 4j
12. 99.99
13. 0
14. 7.5
15. 2e-5

Check your work with type():

# Use type() to verify each classification
print(f"42 is {type(42)}")
print(f"3.14 is {type(3.14)}")
print(f"-5 is {type(-5)}")
print(f"0.5 is {type(0.5)}")
print(f"100 is {type(100)}")
# (Continue for remaining values...)
# Note: Loops for repetitive tasks are covered in Chapter 22

Exercise 2: Fix the Type Errors

Identify the type mismatch in each code snippet. Explain what's wrong, then fix it.

# Snippet 1
student_count: int = 25.7
print(student_count)

# Snippet 2
result: float = 10 / 3
print(f"10 divided by 3 is {result}")

# Snippet 3
total_price: float = 19.99
tax: float = 0.08
final_price: int = total_price * (1 + tax)

# Snippet 4
age: int = 5 / 2
print(age)

Exercise 3: Type Inspection

Use type() to verify the type of each calculation. Predict the type BEFORE running code, then verify:

# Predict type, then check:
a = 10 + 5
b = 10.0 + 5
c = 10 + 5.0
d = 10 / 2
e = 10 // 2
f = 10.0 / 2
g = type(10) # Special: what's the type of type()?

# Check each one (loops covered in Chapter 22)
print(f"{a} has type {type(a)}")
print(f"{b} has type {type(b)}")
print(f"{c} has type {type(c)}")
print(f"{d} has type {type(d)}")
print(f"{e} has type {type(e)}")
print(f"{f} has type {type(f)}")
print(f"{g} has type {type(g)}")

Try With AI

Ready to master when to use int vs. float vs. complex in real applications?

🔍 Explore Numeric Type Tradeoffs:

"Compare int, float, and complex types. For each, show me 3 real-world scenarios where that type is the ONLY correct choice. Then show me a scenario where choosing the wrong type (like using float for inventory count) causes a bug. Include the actual error or unexpected behavior."

🎯 Practice Type Classification:

"Give me 15 data scenarios (student count, product price, temperature, distance, age, stock price, etc.). For each, I'll choose int or float and explain my reasoning. Then challenge my choices—if I pick float for age, explain why int is better. If I pick int for money, show me the precision problem I'll encounter."

🧪 Test Edge Case Understanding:

"Create Python code demonstrating these 3 edge cases: (1) float precision error with money (0.1 + 0.2), (2) integer division surprise (5 / 2 returns float), and (3) Python's unlimited int size (10**100). For each, explain why it happens and how to handle it correctly."

🚀 Apply to Your Domain:

"I'm building [describe your project]. Help me identify every numeric value I need to store, then classify each as int or float. For financial data, warn me about float precision and suggest alternatives like Decimal or storing cents as int."