Order of Operations

« Back to Glossary Index

If you recall an acronym like PEMDAS or BEDMAS from math class, you might remember order of operations. It tells us what order to evaluate an expression in.

For example, the traditional mathematical order of operations is:

  1. Parentheses (sometimes also called Brackets)
  2. Exponents
  3. Multiplication/Division
  4. Addition/Subtraction

Those operators all exist in programming as well, as well as a few additional ones:

OperatorDescriptionExample
ParenthesesAnything inside ()(2 + 4) / 2 = 3
ExponentsThings like x2. Written as x**2 in code.4**2 + 3 = 19
NegationNegative numbers like -2-3**2 = -9
Multiplication, Division, RemainderMultiplication is represented by * e.g. 2*3, division by / e.g. 2/3, and remainder with % e.g. 2%3. There is also integer division, //3 / 10.0 + 4 = 4.3
Addition and Subtraction+ and -, respectively.2 + 3 - 4 = 1
<<=>=>!===, in, not in, is, is notComparisons, such as identity and membership tests.2 >= 1 + 2 = False
notAs in, not xnot 3 > 5 = True (becomes not False because 3 > 5 is False).
andThe and operator.2 == 2 and 3 > 2 = True
orThe or operator.2 < 1 or 2+1 == 3 = True
A table demonstrating order of operations.

This table is not exhaustive; for the full list, see the Python documentation. That said, it’s doubtful you’ll end up using some of the operators I excluded from the table above in a visual novel game, such as bitwise AND or shifts.

In short, order of operations lets us know which order we should evaluate a statement like the following: 2 + 3 * 4 -> multiplication comes before addition, so we do 3*4 first and end up with 2 + 12 = 14. Without following order of operations, we could have ended up with a different answer like 20.

Ren’Py always follows order of operations when determining the result of an expression, so you should be sure to know what order your operators are being evaluated in so your conditional statement does what you’re expecting.

« Back to Glossary Index