Additional Details on Numbers

Type Coercion

  • Numerical operations only work on numbers of the same type
  • Both operands are converted to the same type if possible.
  • Example: in 3*4.5, "3" is converted to a float before the multiply.

Integer truncation

  • Operations on integers return integers.
  • Division truncates down.
  • So 7/4 -> 1.

Rounding

  • The round() function rounds away from 0 for decimals ending in 5.
  • Example: round(0.5) -> 1.0, round(-0.5) -> -1.0
<<< O'Reilly OSCON 2000, Introduction to Python, Slide 54
July 17, 2000, beazley@cs.uchicago.edu
>>>