COGNITION FOUNDRYLEARN. BUILD. UNDERSTAND.
PY-006 / Making useful calculations◷ 18 min read + practice

Numbers and calculations

Even a small utility needs correct arithmetic. Python has separate operators for ordinary division, floor division, remainders and powers.

By the end, you can: Build and explain numeric expressions, distinguish division operations and avoid assuming every decimal is represented exactly.
Before this lesson: PY-005

Choose the intended operation

+, -, * and / perform addition, subtraction, multiplication and division. // performs floor division; % returns the remainder. ** raises a value to a power. Parentheses control grouping.

For positive whole numbers, 17 // 5 gives 3 complete groups and 17 % 5 gives 2 left over. This is useful for converting an elapsed number of minutes into hours and remaining minutes.

Division and rounding

The / operator produces a floating-point result even when the operands are integers. round(value, digits) rounds a value for display or calculation, but it is not a cure for every numerical precision issue.

For financial or exact base-ten decimal calculations, the decimal module is often more appropriate. We will teach it separately before building tools that require it.

Floating point is an approximation

Many decimal fractions cannot be represented exactly in binary floating point. This is why 0.1 + 0.2 may not equal 0.3 when printed with full precision.

The lesson is not that Python arithmetic is broken. It is that numbers have representations with limits. Choose a type and comparison strategy appropriate to your problem.

Try it yourself.

Choose a starting example or write something completely different. You control the code; the Run button executes what is in the editor, not a hidden preset.

CF Python PlaygroundBrowser-based Python
Ready to load Python
Split minutes into hours

Use floor division and the remainder.

Syntax colours highlight Python code. Tab inserts four spaces; press Escape then Tab to leave the editor. Ctrl+Enter runs the current code.
OUTPUT / ERRORS
Press Run Python to execute the editable code.
NOW MAKE IT YOURS

Try 59, 60 and 121 minutes. Explain each result.

Need a hint?

Both calculations use the same total_minutes.

On first Run, a pinned Python runtime downloads from jsDelivr. Code executes locally in your browser's Web Worker, not on our server. It cannot inspect your normal PC files or Windows hardware. Avoid entering secrets. Heavy/infinite loops are stopped by a time limit.

Check your understanding.

Try answering each question before opening its reference answer. Explain the idea aloud or change an example to test it for yourself.

  1. What are the difference between /, // and %?

    View answer

    / performs true division and generally returns a float (7 / 2 → 3.5). // performs floor division (7 // 2 → 3) and % returns the remainder (7 % 2 → 1). With negative numbers floor division rounds toward negative infinity, not necessarily toward zero.

  2. Why might round(0.1 + 0.2, 1) display 0.3 even though the original sum is not exactly 0.3?

    View answer

    Binary floating-point cannot represent many decimal fractions exactly. 0.1 + 0.2 can therefore be slightly different from 0.3. round(..., 1) rounds the *result* to one decimal place for most common displays; it does not make all floating-point arithmetic exact. Use decimal.Decimal in situations requiring decimal arithmetic with controlled precision.

Where this leads.

Next releases will teach conditions and loops so these calculations can become reusable problem-solving programs.