Reputation: 259
My question looks like this:
10-7//2*3+1
I am supposed to solve the equation.
My answer seems to come out as 8 when using PEMDAS:
But when putting it into python, I get a 2. Why is this so?
It seems the programs order is as such:
I don't get it.
Upvotes: 22
Views: 105628
Reputation: 1
When two operators have the same precedence like *, /, //, % perform the left one first.
For example:
8 * 1 // 5
= 8 // 5
= 1 which is correct.
You may think that
8 * 1 // 5
= 8 * 0
=0 which is not correct.
Note: if there is parentheses and exponent then, parentheses is evaluated first and then exponent depending upon the case
Upvotes: 0
Reputation: 22534
PEMDAS is better expressed as
So in your expression, the division should be done before the multiplication, since it is to the left of the multiplication. After those are done, then do the subtraction then the addition.
Upvotes: 21
Reputation: 52449
How do order of operations go on Python?
I don't know what a PE(MD)(AS)
is, but Python order of operations (order of precedence) rules go as the "Operator Precedence" table from the official documentation says they go. :)
Keep in mind, many of these operator precedence rules are opposite in Python how they are in C or in C++, but here are the "Operator precedence" rules in Python 3:
To read all the footnotes in the table, click on the link just above.
Upvotes: 6
Reputation: 11
I think that python doesn’t follow PEMDAS or BODMAS, unless you put the parentheses/brackets around it. So basically I’ll have to mentally do it before writing the program
Upvotes: 0
Reputation: 1121196
PEMDAS
is P
, E
, MD
, AS
; multiplication and division have the same precedence, and the same goes for addition and subtraction. When a division operator appears before multiplication, division goes first.
The order Python operators are executed in is governed by the operator precedence, and follow the same rules. Operators with higher precedence are executed before those with lower precedence, but operators have matching precedence when they are in the same group.
For 10-7//2*3+1
, you have 2 classes of operators, from lowest to higest:
+, -
(correlating with AS
== addition and subtraction)*, @, /, //, %
(correlating with MD
, so multiplication and division).So //
and *
are executed first; multiplication and division fall in the same group, not a set order here (MD
doesn't mean multiplication comes before division):
10 - ((7 // 2) * 3) + 1
So 7 // 2
is executed first, followed by the multiplication by 3. You then get the subtraction from ten and adding one at the end.
We've glossed over an issue that doesn't affect your particular case, but is very important for writing real Python programs. PEMDAS isn't really about order of operations; it doesn't decide what order things are evaluated in. It's really about argument grouping. PEMDAS says that a + b + c * d
is evaluated as (a + b) + (c * d)
, but it doesn't say whether a + b
or c * d
is evaluated first.
In math, it doesn't matter what you evaluate first, as long as you respect the argument grouping. In Python, if you evaluted b()
and c()
first in a() + (b() + c())
just because they're in parentheses, you could get a completely different result, because Python functions can have side effects.
Python expression evaluation mostly works from left to right. For example, in a() + b() + (c() * d())
, evaluation order goes as follows:
a()
b()
+
, now that its arguments are readyc()
d()
*
, now that its arguments are ready+
, now that its arguments are readyThis is despite the high precedence of *
and the parentheses around the multiplication.
Upvotes: 35
Reputation: 5131
This is documented here (Python Documentation / Expressions / Operator-Precedence):
Upvotes: 4