Programming in Python · Lesson 4

Numeric Data Types and Expressions — Programming Examples

Five runnable Python examples covering numeric literals, arithmetic and compound operators, order of operations, coercion, type casting, and floating-point formatting.

  • numeric-types
  • operators
  • order-of-operations
  • type-casting
  • formatting

Use these short, runnable examples as demonstration checkpoints. Ask students to predict the output before you run each one.

  1. Numeric literals and primitive data types
whole_number = 128
temperature = -4.5
scientific_notation = 4.5967e2   # equivalent to 459.67

print(whole_number)
print(temperature)
print(scientific_notation)

print(type(whole_number))
print(type(temperature))

Teaching point: A number without a decimal point is an integer; a number with one (or written in E notation) is a floating-point value. type() confirms which primitive data type Python assigned.

  1. Arithmetic operators and order of operations
quiz1 = 82
quiz2 = 90
quiz3 = 76

# Order matters: without parentheses, * runs before the second + finishes
wrong_total = quiz1 + quiz2 + quiz3 * 3
correct_total = (quiz1 + quiz2 + quiz3) * 3

average = correct_total / 3        # true division always returns a float
whole_average = correct_total // 3 # floor division drops the remainder
remainder = correct_total % 3      # modulo returns what floor division drops

print(wrong_total)
print(correct_total)
print(average)
print(whole_average)
print(remainder)

Teaching point: multiplication and division run before addition and subtraction, left to right, and parentheses are the only way to force a different order. Once total is correct, / always returns a float, // keeps only the whole part, and % returns what // throws away.

  1. Compound assignment operators
attendance = 20
attendance += 3   # three more students arrive
attendance -= 1   # one leaves early
attendance *= 2   # count is doubled for a two-session day
attendance %= 7   # keep only the remainder after dividing by a week

print(attendance)

Teaching point: each compound operator combines an arithmetic operation with reassignment. Trace attendance’s value after every line: 23, 22, 44, then 2.

  1. Coercion, type casting, and the rounding quirk
number_of_albums = 3
individual_price = 12.99

total_cost = number_of_albums * individual_price   # coercion: int promoted to float for this calculation
print(total_cost)
print(type(number_of_albums))   # still an int -- coercion never changes the variable itself
print(type(total_cost))

# // truncates before the multiplication below, losing information
short_way = 1 // 2 * 2
print(short_way)

# Casting AFTER the full calculation keeps the intermediate precision
accurate_way = int(1 / 2 * 2)
print(accurate_way)

Teaching point: Python coerces an int into a float only for the calculation; the original variable’s type never changes. // discards the fractional part immediately, so multiplying afterward can’t recover it — casting with int() only after finishing the real division gives the expected result.

  1. Floating-point precision and formatting output: purchase total calculator
item_price = float(input("Price per item: "))
quantity = int(input("Quantity: "))

subtotal = item_price * quantity
tax = subtotal * 0.07
total = subtotal + tax

# Floating-point math can carry tiny rounding errors (e.g. 0.1 + 0.2 != 0.3 exactly);
# format() hides those extra digits without changing the stored value.
print("Subtotal: ${:.2f}".format(subtotal))
print("Tax:      ${:.2f}".format(tax))
print("Total:    ${:.2f}".format(total))

Build sequence:

  1. Start with hard-coded price and quantity values.
  2. Replace them with input(), converting to float and int.
  3. Calculate subtotal, tax, and total using arithmetic operators.
  4. Format each line to two decimal places with format().

Exit-ticket prompts

  1. Evaluate: 2 + 3 * 4
  2. Write the compound-assignment equivalent of: score = score - 5
  3. Trace: total = 10; total //= 3; total %= 2; print(total)
  4. Explain why 0.1 + 0.2 does not print exactly 0.3
  5. What data type will type(price) report for price = 19.99?

Answer key

  1. 14
  2. score -= 5
  3. total becomes 3 after total //= 3 (10 // 3), then 1 after total %= 2 (3 % 2); prints 1
  4. Binary floating-point storage can’t represent 0.1 or 0.2 exactly, so the sum carries a tiny rounding error
  5. <class ‘float’>