Python Forum

Full Version: Confused by this modulo equation
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
This is probably a simple question for experienced users...I'm just starting with Python and I'm working with modulo equations. The tutorial I'm using had this equation: (100 - 25 * 3 % 4), and the result is 97. How to explain that? As I understand it, we take the first part of the equation first, so (100 - 25 * 3). When I run that by itself I get 25. And (25 % 4) results in 1. So where do they get 97?
modulo operator has the same priority as division
Output:
In [47]: 25 * 3 % 4 Out[47]: 3
The result is right.

If it is unclear - the evaluation order is
100 - ((25 * 3) % 4)
Every language has a operator-precedence.
The most manuals shows the order from high priority to low priority. It's reversed in Pythons documentation.

When operators do have the same precedence, they are evaluated from left to right.
Okay, I finally understand, I had the operator precedence wrong. So 25*3 = 75, then we do 75 % 4 = 3, THEN we finally subtract 3 from 100, to get 97. I'll have to practice, ha ha.

Thanks for the help!