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(usebool) - Building arrays/lists →
int(for indices)
NOW Show Integer Code
Here's how you declare integer variables with type hints:
Loading Python environment...
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):
Loading Python environment...
💬 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-3means 0.0015 (small) or2.5e6means 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(useint)
NOW Show Float Code
Here's how you declare float variables:
Loading Python environment...
Verify the type with type():
Loading Python environment...
🎓 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:
| Scenario | Type | Why |
|---|---|---|
| Age (years) | int | No fractional ages exist |
| Price (dollars and cents) | float | Needs decimal precision |
| Student count | int | Can't have 25.5 students |
| Distance traveled (km) | float | Can be 3.7 km, not just 3 or 4 |
| Number of items sold | int | Discrete count |
| Average score (points) | float | May not be whole (87.5 average) |
| Inventory items | int | Counting whole units |
| Weight in kilograms | float | Continuous measurement |
| Array index | int | Always whole number |
| Calculation result (ratio) | float | May 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
Loading Python environment...
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)
Loading Python environment...
Why it matters: You lose information. The .5 disappears.
Fix: Use float when decimals are involved.
Loading Python environment...
Mistake 2: Integer Division Surprise
Loading Python environment...
Why it happens: In Python 3, / always returns a float, even if both numbers are integers.
Fix: Use // for integer division (discards the decimal):
Loading Python environment...
Mistake 3: Float Precision Issues (Advanced)
Loading Python environment...
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:
Loading Python environment...
Mistake 4: Using Float for Money (Anti-Pattern)
Loading Python environment...
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).
Loading Python environment...
Mistake 5: Confusing Type Hints with Type Conversion
Loading Python environment...
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():
Loading Python environment...
Exercise 2: Fix the Type Errors
Identify the type mismatch in each code snippet. Explain what's wrong, then fix it.
Loading Python environment...
Exercise 3: Type Inspection
Use type() to verify the type of each calculation. Predict the type BEFORE running code, then verify:
Loading Python environment...
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."