Skip to main content

3.10) Arithmetic Operations


If you use Python for your research, you might want to do some arithmetic. We’ve already learned how to do some of these. Here are the main operations (called arithmetic expressions) you can do in Python:

  • Addition: a+b
  • Subtraction : a-b
  • Multiplication : a*b
  • Division : a/b
  • Power: a**b
  • Modulo: a%b [More info: http://en.wikipedia.org/wiki/Modulo_operation ]
  • Truncating division: a//b (truncates the result of a division to an integer, also called floor division)

If you use several operators in one operation, the order of priority is the traditional order of operation in arithmetics: (i) exponent, (ii) multiplication, division and modulo, (iii) addition, subtraction. Use round brackets ( and ) to prioritise operations. Spaces in between numbers and operators do not matter.

For example, try out the following in the Python interpreter:

>>> 2 - 3
>>> 2*3 + 5
>>> 2 * (3+5)
>>> 2**3
>>> 2.0/3.0