MEPX
Chapter 5 of 15All chapters

Chapter 5 of 15

Numbers and maths

Integer and float behaviour, and the two divisions.

Two divisions

The / operator always produces a float, so 4 / 2 is 2.0. The // operator floors the result towards negative infinity, so 3 // 2 is 1 and -3 // 2 is -2.

  • % gives the remainder and ** raises to a power.
  • Integers have no size limit in Python, so large factorials just work.

Floats are approximate

Floats follow the same binary rules as every other language, so 0.1 + 0.2 does not print as 0.3. Use round for display and the decimal module when exactness matters, such as money.