Mathematics

Lesson Progress
0% Complete

The four basic mathematical operators in Python are + - * /, however, others also exist to provide increased flexibility.

  • + – addition
  • - – subtraction
  • * – multiplication
  • / – division
  • // – integer division
  • ** – exponentiation
  • % – modulo

Operators can be combined with numeric values to form an expression which can be evaluated by Python. For example, typing 2 + 1 will yield 3 in the interpreter. Below are some more examples.

>>> 2 + 1
3
>>> 1 + 10 -2
9
>>> 2 * 3 * 4
24
>>> 2 * 6 / 3
4.0
>>> 6 / 0
ZeroDivisionError: division by zero

Critical points

  • Division by zero will yield an error, as seen above
  • Regular division (/) yields a float; for an integer value, use integer division (//)

Order of precedence

Python follows the standard BIDMAS order of precedence for operations – brackets, indices, division/multiplication, addition/subtraction. For example, 60 / 2 * 3 yields 90.0 while 60 / (2 * 3) yields 10.0.