Numbers and Arithmetic
Unit ID: M02-U02 Estimated active time: 22-30 minutes
Integers and floating-point numbers
Python commonly uses:
intfor whole numbers, such as3,0, and-12;floatfor decimal-style numbers, such as2.5and0.75.
Use type() to inspect them:
print(type(3))
print(type(3.0))
Core arithmetic operators
print(10 + 3) # addition
print(10 - 3) # subtraction
print(10 * 3) # multiplication
print(10 / 3) # division
print(10 // 3) # floor division
print(10 % 3) # remainder
print(10 ** 3) # exponentiation
Expected results:
13
7
30
3.3333333333333335
3
1
1000
The / operator produces a floating-point result. Floor division // gives the floored whole-number result. % gives the remainder.
Choose an operation by meaning
Suppose 32 course hours are shared equally across 10 study days.
course_hours = 32
study_days = 10
hours_per_day = course_hours / study_days
print(hours_per_day)
The result is 3.2 hours per day.
If you need complete 3-hour blocks and remaining hours:
complete_blocks = course_hours // 3
remaining_hours = course_hours % 3
print(complete_blocks)
print(remaining_hours)
The results are 10 complete blocks and 2 remaining hours.
Order and parentheses
subtotal = 800
tax_rate = 18 / 100
total = subtotal + subtotal * tax_rate
print(total)
The result is 944.0. Parentheses can make the intended grouping clearer:
total = subtotal * (1 + tax_rate)
Do not round automatically unless the task defines a rounding rule. Display formatting and calculation precision are different decisions.
Floating-point caution
Run:
print(0.1 + 0.2)
You may see 0.30000000000000004. Many decimal fractions cannot be represented exactly in binary floating-point form. This is normal computer behaviour, not proof that Python cannot calculate.
For ordinary course examples, compare expected ranges or use an appropriate rounding rule. Financial systems may require decimal arithmetic designed for exact base-10 values; that is outside this beginner module.
Practice
A fictional course has 9 units, each estimated at 25 minutes.
Calculate:
- total minutes;
- whole hours using floor division;
- remaining minutes using
%; and - exact hours using
/.
Expected results: 225 minutes, 3 whole hours, 45 remaining minutes, and 3.75 exact hours.
Takeaway
Use numeric operators according to the task's meaning. Inspect types, make grouping visible, and treat floating-point output with appropriate care. Next, we will create boolean values by asking comparison questions.
