Programming and Computation · Module 2 of 10

Numbers, Types, and Precision

Python has two number types, integers and floats, and they behave differently when you divide. This module covers true and floor division, the remainder, floating-point precision, and how to check and convert types.

01

Readiness check

This module is about how Python stores numbers. Tick only what you can do closed-notes.

  • Recall the difference between a whole number and a decimal.
  • Recall a remainder from long division.
  • Use Python as a calculator from Module 1.
  • Recall that measurements have limited precision.
  • Round a number to a set number of decimals.
0 or 1 weak itemsContinue with this module.
2 weak itemsRevisit the calculator in Module 1.
3 or more weak itemsRevisit significant digits in Orientation, Module 3.
02

The core idea

Python numbers are either integers or floats. True division / always gives a float; floor division // gives the whole-number quotient; the modulo % gives the remainder. Floats carry finite precision, so tiny rounding errors are normal, and type() tells you what you have.

7 / 2 gives 3.5 (float)7 // 2 gives 3 ; 7 % 2 gives 1type(x) reveals int or float

Every value in Python has a type, and for numbers the two you meet first are the integer (a whole number, like 7) and the float (a decimal, like 3.5). The type matters most when you divide. True division with a single slash, 7 / 2, always produces a float, 3.5, even when the result is whole. Floor division with a double slash, 7 // 2, throws away the fractional part and gives the whole-number quotient, 3, and the modulo operator, 7 % 2, gives what is left over, the remainder 1. Together, // and % answer "how many whole times, and how much left" questions, useful for things like splitting a count into groups. The second essential fact is that floats are stored in binary with finite precision, so a calculation like 0.1 + 0.2 returns 0.30000000000000004, not exactly 0.3. This is not a bug; it is how computer arithmetic works, and the fix is to compare and display with rounding rather than expecting exact equality. Knowing an object's type, checked with type(x), and converting deliberately with int() or float(), prevents a whole class of silent numerical mistakes.

The skill works when: you pick /, //, or % deliberately and round floats when comparing.
The skill breaks down when: floor division is used by accident, or floats are expected to be exactly equal.
The concept. One slash divides to a float; two slashes give the whole quotient; the percent sign gives the remainder. Choosing the right one is the skill.
03

The skills, taught in order

Five skills keep numerical work correct.

2.1 Integers and floats

An integer is a whole number; a float carries a decimal. Most engineering quantities are floats. Mixing an int and a float in arithmetic produces a float, which is usually what you want.

2.2 True division and floor division

A single slash / always gives a float, so 10 / 2 is 5.0. A double slash // gives the whole-number quotient, so 10 // 3 is 3. Use floor division only when you genuinely want to discard the remainder.

2.3 The modulo operator

The percent sign % returns the remainder: 10 % 3 is 1. It answers "is this divisible?" (a remainder of 0) and "which bin does this fall in?" questions.

OperationExampleResult
True division7 / 23.5 (float)
Floor division7 // 23 (int)
Modulo7 % 21 (remainder)

The three division operators and exactly what each returns.

2.4 Floating-point precision

Floats store values in binary with finite precision, so results can carry tiny errors, and 0.1 + 0.2 is not exactly 0.3. Never test floats for exact equality; compare with a tolerance or round for display.

2.5 Checking and converting types

Call type(x) to see whether a value is an int or a float, and convert deliberately with int(x) (which truncates toward zero) or float(x). Deliberate conversion prevents surprises.

Engineering connection: reading a sensor count and splitting it into whole units and a remainder uses // and %, while any physical calculation uses floats and their rounding.

04

Worked example 1: the three divisions

Show how Python divides 7 by 2 three different ways, and confirm the types.

print(7 / 2)      # true division
print(7 // 2)     # floor division
print(7 % 2)      # remainder
print(type(7 / 2), type(7 // 2))

Output:

3.5
3
1
<class 'float'> <class 'int'>
  1. ProblemDivide 7 by 2 with each operator and check the result types.
  2. Given / find7 and 2. Find the three results and their types.
  3. Model/ true division, // floor division, % remainder.
  4. Solve7 / 2 = 3.5 (float); 7 // 2 = 3 (int); 7 % 2 = 1.
  5. Check3 whole twos fit in 7 with 1 left over, and 3 times 2 plus 1 is 7.
  6. ConclusionTrue division keeps the fraction; floor division and modulo split a number into whole part and remainder.
Result. 3.5 ; 3 ; 1 (float, int, int).
05

Worked example 2: kinetic energy as a float

Compute the kinetic energy of a 4 kg mass at 3 m/s, one half m v squared, and note the type.

m = 4
v = 3
ke = 0.5 * m * v ** 2
print(ke, type(ke))

Output:

18.0 <class 'float'>
  1. ProblemFind the kinetic energy and its type.
  2. Given / findm = 4 kg, v = 3 m/s. Find KE = one half m v squared.
  3. Modelke = 0.5 * m * v ** 2.
  4. Solveke = 0.5 × 4 × 9 = 18.0 J, a float because 0.5 is a float.
  5. CheckHalf of 4 is 2, times 9 is 18, matching.
  6. ConclusionMultiplying by the float 0.5 makes the whole result a float, the normal state for physical quantities.
Result. ke = 18.0 J (a float).
06

Misconceptions and diagnostics

MistakeSymptomDiagnostic questionCorrection
Using // by accidentFraction silently discarded"Did I want the remainder gone?"Use / unless you truly want the whole quotient.
Expecting exact floats0.1 + 0.2 fails an equality test"Am I comparing floats exactly?"Compare with a tolerance or round for display.
int() rounds3.9 becomes 3, not 4"Truncate or round?"int() truncates; use round() to round.
Ignoring typeSurprising division behavior"What does type(x) say?"Check the type when results look wrong.
07

Practice ladder

Level 1 · Direct skill

Predict and check 17 // 5 and 17 % 5.

Show answer

17 // 5 = 3 and 17 % 5 = 2, since 3 fives are 15 with 2 left over.

Level 2 · Mixed concept

Predict int(3.9) and round(3.9).

Show answer

int(3.9) = 3 (truncates); round(3.9) = 4 (rounds).

Level 3 · Independent problem

Run 0.1 + 0.2 and then round(0.1 + 0.2, 2). Explain the difference.

Show answer

The first gives 0.30000000000000004 from float precision; the second gives 0.3. Round for display or comparison, do not test exact equality.

Transfer task | Real engineering

You have 1000 bolts and pack 24 per box. Compute the number of full boxes and the leftover bolts.

What good work looks like

1000 // 24 = 41 full boxes, and 1000 % 24 = 16 leftover bolts. A good answer uses floor division and modulo and checks 41 × 24 + 16 = 1000.

08

Working with AI, and proving it yourself

Use AI as a tutor, not a black box

"Explain why 0.1 + 0.2 is not exactly 0.3 in Python."
"Give me five division expressions; I will predict int vs float, then run them."
"Fix my float comparison." Learn to use a tolerance yourself.
"What type is this?" Predict, then check with type().

Portfolio task

Write a short script that splits a total count into whole groups and a remainder, and prints both with a rounding-safe check.

Must include: //, %, a type() check, and one float rounded for display.
09

Retrieval and spaced review

Closed notes. Answer out loud, then reveal.

1. What does 7 / 2 return, and what type?

3.5, a float.

2. What do // and % give?

The whole quotient and the remainder.

3. Why is 0.1 + 0.2 not exactly 0.3?

Floats have finite binary precision, so tiny rounding errors appear.

4. int(3.9) versus round(3.9)?

3 (truncates) versus 4 (rounds).

5. How do you see a value's type?

Call type(x).

TodayFinish this quiz and Levels 1 and 2 of the ladder.
+1 dayRe-derive the boxes-and-leftover example.
+3 daysExplain float precision to a peer.
+7 daysMove on to input and output in Module 3.
+30 daysChoose the right division operator without thinking.
10

Textbook mapping

This module follows Hans Fangohr, Introduction to Python for Computational Science and Engineering. Use these references to read further.

Topic in this moduleWhere to read more
Integer division and moduloFangohr, Section 2.3, Integer division
Numbers and typesFangohr, Sections 3.1 and 3.2
Floating-point behaviorFangohr, Chapter 3, Numbers

Chapter numbers refer to Fangohr's Introduction to Python for Computational Science and Engineering. Any recent version is equivalent for study.