AI Skill Report Card

Working with Python Numeric Types

B+78·Aug 12, 2026·Source: Web
13 / 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
13 / 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 //, ** vs pow())
  • 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 int for counts, indices, exact values.
  • Use float for measurements, results of division, scientific computation.
  • Use complex only when working with imaginary components (signal processing, engineering math).

2. Choose operators deliberately

OperatorMeaningExample
+ - *standard arithmetic3 * 4 -> 12
/true division (always float)5 / 2 -> 2.5
//floor division5 // 2 -> 2, -5 // 2 -> -3
%modulo (sign follows divisor)-5 % 2 -> 1
**exponentiation2 ** 10 -> 1024
divmod(a, b)(a//b, a%b) in one calldivmod(5,2) -> (2,1)

3. Convert explicitly

Python
int(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

Python
z = 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
14 / 20

Example 1: Input: Compute average of a list of ints and format to 2 decimal places. Output:

Python
nums = [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:

Python
0.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:

Python
int(-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
  • Prefer / for math that should return float; use // only when floor semantics are intended.
  • Use math.isclose() instead of == for float comparisons.
  • Use decimal.Decimal or fractions.Fraction instead 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 bool is a subclass of int (True == 1), which affects arithmetic and can cause subtle bugs.
  • 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 complex into ordering comparisons (<, >) — complex numbers are unorderable and will raise TypeError.
0
Grade B+AI Skill Framework
Scorecard
Criteria Breakdown
Quick Start
13/15
Workflow
13/15
Examples
14/20
Completeness
12/20
Format
13/15
Conciseness
13/15