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:
- Parentheses (sometimes also called Brackets)
- Exponents
- Multiplication/Division
- Addition/Subtraction
Those operators all exist in programming as well, as well as a few additional ones:
Operator | Description | Example |
---|---|---|
Parentheses | Anything inside () | (2 + 4) / 2 = 3 |
Exponents | Things like x2. Written as x**2 in code. | 4**2 + 3 = 19 |
Negation | Negative numbers like -2 | -3**2 = -9 |
Multiplication, Division, Remainder | Multiplication 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 not | Comparisons, such as identity and membership tests. | 2 >= 1 + 2 = False |
not | As in, not x | not 3 > 5 = True (becomes not False because 3 > 5 is False). |
and | The and operator. | 2 == 2 and 3 > 2 = True |
or | The or operator. | 2 < 1 or 2+1 == 3 = True |
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