##### Sort methods
 Sort methods rs74 Silly Frenchman Posts: 23 Threads: 7 Joined: Jun 2020 Reputation: Jun-12-2020, 07:26 AM ```a = add(multi(3)); a = a + mod(div(a + 3)) + sub(12)``` How (and why) should this functions (add, multi, mod, div, sub) be sorted? I am thinking like this -> 1. multi (a = a) (first function) 2. mod (a + mod) (second function) 3. sub (a + mod + sub) 4. ? 5. ? Reply Posts: 7,567 Threads: 133 Joined: Sep 2016 Reputation: Jun-12-2020, 07:32 AM Not clear what you ask. What do you mean by "sorting"? Are these functions from operator module? If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein How to Ask Questions The Smart Way: link and another link Create MCV example Debug small programs Reply rs74 Silly Frenchman Posts: 23 Threads: 7 Joined: Jun 2020 Reputation: Jun-12-2020, 07:50 AM Which function is the first to run? These functions should just be sorted (by me in general, not the program) in a list in the way they are executed(?). Example: multi mod sub div add Reply Posts: 7,567 Threads: 133 Joined: Sep 2016 Reputation: Jun-12-2020, 08:16 AM (This post was last modified: Jun-12-2020, 08:21 AM by buran.) Read evaluation order and operator precedence Quote:Python evaluates expressions from left to right. Notice that while evaluating an assignment, the right-hand side is evaluated before the left-hand side. So in your example: it will first evaluate `3`, resulting in 3 then it will evaluate `multi(3)` - this will throw error if `multi` expects 2 arguments, see below then it will evaluate `add(result from multi(3))` then it will assign the result from item 3 to `a` then, on next line, it evaluate `a` - first term on the right-hand side. then it will evaluate a+3 then it will evaluate `div(result from a + 3)` then it will evaluate mod(result from div(result from a + 3)) then it evaluate 12 then it will evaluate sub(12) then it will evaluate `a + result from mod(result from div(result from a + 3)) = result from sub(12)` then will assign the result to `a` You did not confirm but, I would assume these are functions from operator module, although there is no `multi`, but `mul`. So, first of all, your example is invalid, because all these functions expect 2 arguments. e.g. mul(3) will throw error ```>>> from operator import mul >>> mul(3) Traceback (most recent call last): File "", line 1, in TypeError: mul expected 2 arguments, got 1``` If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein How to Ask Questions The Smart Way: link and another link Create MCV example Debug small programs Reply rs74 Silly Frenchman Posts: 23 Threads: 7 Joined: Jun 2020 Reputation: Jun-12-2020, 08:20 AM (Jun-12-2020, 08:16 AM)buran Wrote: Read evaluation order and operator precedence Quote:Python evaluates expressions from left to right. Notice that while evaluating an assignment, the right-hand side is evaluated before the left-hand side. So in your example: it will first evaluate `(3)`, resulting in 3 then it will evaluate `multi(3)` - this will throw error if `multi` expects 2 arguments, see below then it will evaluate `add(result from multi(3))` then it will assign the result from item 3 to `a` then, on next line, it evaluate `a` - first term on the right-hand side. then it will evaluate a+3 then it will evaluate `div(result from a + 3)` then it will evaluate mod(result from div(result from a + 3)) then it evaluate 12 then it will evaluate sub(12) then it will evaluate `a + result from mod(result from div(result from a + 3)) = result from sub(12)` then will assign the result to `a` You did not confirm but, I would assume these are functions from operator module, although there is no `multi`, but `mul`. So, first of all, your example is invalid, because all these functions expect 2 arguments. e.g. mul(3) will throw error ```>>> from operator import mul >>> mul(3) Traceback (most recent call last): File "", line 1, in TypeError: mul expected 2 arguments, got 1``` Thanks. Yes, will read about evaluation order and operator precedence. Reply DeaD_EyE Da Bishop Posts: 1,640 Threads: 6 Joined: May 2017 Reputation: Jun-12-2020, 08:36 AM (This post was last modified: Jun-12-2020, 08:37 AM by DeaD_EyE.) Code execution is from left to right from top to down. If the left or right operand is a function-call, then this is evaluated first. The operator precedence is what you want to know: https://docs.python.org/3/reference/expr...precedence My code examples are always for Python >=3.6.0 Almost dead, but too lazy to die: https://sourceserver.info All humans together. We don't need politicians! Reply

Forum Jump:

### User Panel Messages

##### Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020