AI Skill Report Card
Working with Python Numeric Types
Quick Start13 / 15
Python# Three numeric types: int, float, complex a = 5 # int b = 3.14 # float c = 2 + 3j # complex # Mixed arithmetic promotes to the "wider" type: int -> float -> complex result = a + b # 8.14 (float) result2 = a + c # (7+3j) (complex) # True division always returns float print(7 / 2) # 3.5 print(7 // 2) # 3 (floor division) print(7 % 2) # 1 (modulo) print(divmod(7, 2)) # (3, 1)
Recommendation▾
Fix typo 'debugges' in description and tighten it to be more concise
Workflow13 / 15
When writing or debugging numeric code, work through this checklist:
Progress:
- Identify which numeric type each value should be (int/float/complex)
- Choose the correct operator for the intended semantics (
/vs//,**vspow()) - Handle type promotion/conversion explicitly where precision matters
- Guard against division-by-zero and float precision pitfalls
- Use appropriate formatting/rounding for output
1. Identify the right type
- Use
intfor counts, indices, exact values. - Use
floatfor measurements, results of division, scientific computation. - Use
complexonly when working with imaginary components (signal processing, engineering math).
2. Choose operators deliberately
| Operator | Meaning | Example |
|---|---|---|
+ - * | standard arithmetic | 3 * 4 -> 12 |
/ | true division (always float) | 5 / 2 -> 2.5 |
// | floor division | 5 // 2 -> 2, -5 // 2 -> -3 |
% | modulo (sign follows divisor) | -5 % 2 -> 1 |
** | exponentiation | 2 ** 10 -> 1024 |
divmod(a, b) | (a//b, a%b) in one call | divmod(5,2) -> (2,1) |
3. Convert explicitly
Pythonint(3.9) # 3 (truncates toward zero, NOT rounds) int("42") # 42 float(3) # 3.0 complex(1, 2) # (1+2j) round(3.567, 2) # 3.57 (banker's rounding on .5 boundaries)
4. Know int-specific tools
Python(10).bit_length() # 4 (255).to_bytes(2, 'big') # b'\x00\xff' int.from_bytes(b'\xff', 'big') # 255 (-7).__abs__() # 7, or just abs(-7)
5. Know float-specific tools
Python(3.75).as_integer_ratio() # (15, 4) (3.75).is_integer() # False float('inf'), float('nan') # special values import math math.isclose(0.1 + 0.2, 0.3) # True (handles float precision)
6. Know complex-specific tools
Pythonz = 3 + 4j z.real # 3.0 z.imag # 4.0 z.conjugate() # (3-4j) abs(z) # 5.0 (magnitude)
Recommendation▾
Examples are somewhat basic/reference-like rather than demonstrating realistic debugging scenarios with clear before/after failure states
Examples14 / 20
Example 1: Input: Compute average of a list of ints and format to 2 decimal places. Output:
Pythonnums = [1, 2, 3, 4] avg = sum(nums) / len(nums) # 2.5 (float, true division) print(f"{avg:.2f}") # "2.50"
Example 2: Input: Check whether 0.1 + 0.2 == 0.3 fails and fix it. Output:
Python0.1 + 0.2 == 0.3 # False, due to float precision import math math.isclose(0.1 + 0.2, 0.3) # True — correct check
Example 3: Input: Convert a negative float to int and explain the result. Output:
Pythonint(-3.9) # -3, truncates toward zero (not floor) import math math.floor(-3.9) # -4, use this if flooring is intended
Recommendation▾
Add a bad-outcome example showing a common numeric bug in a larger code snippet, not just isolated expressions
Best Practices
- Prefer
/for math that should return float; use//only when floor semantics are intended. - Use
math.isclose()instead of==for float comparisons. - Use
decimal.Decimalorfractions.Fractioninstead of float when exact precision matters (money, ratios). - Use f-strings with format specs (
:.2f,:,,:e) for numeric display instead of manual string building. - Remember
boolis a subclass ofint(True == 1), which affects arithmetic and can cause subtle bugs.
Common Pitfalls
- Assuming
int()rounds — it truncates toward zero, not floors or rounds. - Assuming
//always floors — it does, but for negative numbers this differs from truncation (-5 // 2 == -3, not-2). - Using
==to compare floats after arithmetic. - Forgetting that
%result sign follows the divisor's sign in Python (differs from some other languages). - Mixing
complexinto ordering comparisons (<,>) — complex numbers are unorderable and will raiseTypeError.