Python Forum
Confused by this modulo equation - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Confused by this modulo equation (/thread-12646.html)



Confused by this modulo equation - VikramSuh - Sep-05-2018

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?


RE: Confused by this modulo equation - volcano63 - Sep-05-2018

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)


RE: Confused by this modulo equation - DeaD_EyE - Sep-05-2018

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.


RE: Confused by this modulo equation - VikramSuh - Sep-05-2018

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!