Programming in Python · Lesson 4

Numeric Data Types and Expressions

Use integers, floating-point values, arithmetic operators, conversions, order of operations, and formatted numeric output.

  • numeric-data
  • expressions
  • operators
  • type-conversion
  • formatting

Lesson purpose

Numeric data types let Python store quantities and evaluate mathematical expressions. Students learn to choose integers or floats, predict operator results, convert values deliberately, and format output without changing the stored data.

Learning objectives

  • Distinguish primitive numeric data, integers, and floating-point values.
  • Use arithmetic, integer-division, modulo, and compound-assignment operators.
  • Apply order of operations and parentheses.
  • Distinguish coercion from explicit type casting.
  • Explain floating-point approximation and format output with format().

1. Numeric data types

Python provides primitive types directly, including integers and floating-point numbers. Integers are whole numbers and may be positive or negative. Floats contain a decimal point or use E notation.

students = 23
temperature = -4.5
scientific = 4.5967e2

Python floats use double precision. Money and other values requiring exact decimal rules need a deliberate representation; do not assume that a binary floating-point value is exact.

2. Arithmetic expressions

An expression has a value and may contain operands and operators.

total = 10 + 8
difference = 10 - 8
product = 10 * 8
quotient = 10 / 8       # 1.25
whole_quotient = 10 // 8  # 1
remainder = 10 % 8      # 2

Use % to test divisibility or determine even/odd status. Use // when the fractional part is intentionally discarded.

3. Order of operations and compound assignment

Python evaluates parentheses first, then exponentiation, multiplication/division/floor-division/modulo from left to right, and finally addition/subtraction from left to right.

answer = 10 - 8 / 2 * 4 + 5  # -1.0
grouped = (10 - 8) / (2 * 4) + 5

Parentheses make intent visible. Compound operators combine an operation with assignment:

counter = 10
counter += 1
counter *= 2
counter %= 3

4. Coercion and type casting

Coercion is Python’s automatic choice of a compatible type during an operation; it does not permanently change the variable. Type casting is an explicit conversion.

whole = 7
decimal = 2.5
result = whole + decimal  # coercion produces a float

truncated = int(4.9)      # 4; int() truncates, it does not round
converted = float(7)      # 7.0

Binary floating-point storage is an approximation, so results such as 0.1 + 0.2 may not equal a decimal representation exactly. Use appropriate rounding or decimal techniques when the application requires exact financial behavior.

5. Formatting output

Formatting changes how a value is displayed without changing the value stored in a variable. The format() method uses braces as insertion points and parameters for type, precision, width, and alignment.

price = 4.43050
print("Price: ${:.2f}".format(price))
print("Count: {:d}".format(7))
print("Right aligned: {:>10}".format(42))

Do not type-cast a float merely to remove trailing zeros: that discards the fractional data. Format it instead.

Classroom application

  1. Predict the result of expressions using /, //, and % before running them.
  2. Add parentheses to make a multi-step expression match a written formula.
  3. Demonstrate int(4.9) and discuss why it differs from rounding.
  4. Build a small receipt that keeps numeric values unchanged but formats prices to two decimal places.

Common misconceptions

  • / normally produces a float; // discards the fractional portion.
  • % returns a remainder, not a percentage.
  • Type casting changes a value; formatting changes its display.
  • A float’s printed appearance does not guarantee exact decimal storage.

Lesson summary

Choose numeric types intentionally, use operators with a predictable evaluation order, convert values only when needed, and format output so readability does not require sacrificing data.