Reputation: 11
I am doing some very basic excersizes in python. I'm using the hard way to learn python book, which in excersize 3 have a expression I should understand.
3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6, is coming out as 7.
For me, the answer is 6.
6 - 5 + 0 - 1 / 4 + 6
1 - 1 / 4 + 6
6.
This is clearly wrong, but can anyone help me with priorty in mathematics etcetera? I seem to have forgotten everything if it's not inside a parenthesis!
EDIT: Thank you very much for the response. I've clearly learned something about the very basic stuff, which I think is important before moving on! My order of operations was definately way off!
Upvotes: 1
Views: 2217
Reputation: 2814
1/4
rounds down (or floors) to 0
since 1 and 4 are integers.
Therefore 1-0+6 = 7
Upvotes: 2
Reputation: 21548
Priority is for division and multiplication, if you don't use brackets!
In this way: 1-1/4+6=7 because 1/4, working with integer, is = 0
Upvotes: 1
Reputation: 1724
3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6
The order of precedence for this is multiplication and division evaluated left to right and then addition and subtraction. So..
3 + 2 + 1 - 5 + (4 % 2) - (1 / 4) + 6
The ones inside the parentheses are evaluated first because of order of operations. Because you only have integers present (and not a float which would allow the value 0.25), 1/4 will floor to 0 because of python.
3 + 2 + 1 - 5 + 0 - 0 + 6
Afterwards, when you evaluate from left to right for addition/subtraction, you are left with:
6 - 5 + 6 => 1 + 6 => 7
Remember that division and multiplication are evaluated before addition and subtraction. For more information, Order of Operations - Wikipedia
Upvotes: 0
Reputation: 193814
If we look at the rules for Operator precedence in Python we can see that:
3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6
is being treated as:
3 + 2 + 1 - 5 + (4 % 2) - (1 / 4) + 6
(For the arithmetic operators this is the same order as the standard order of operations in mathematics.)
Now 4 % 2
is 0
since the remainder when dividing 4 by 2 is 0. 1 / 4
is also 0
as Python will return a value of the same type as the operands and 0.25 when "floored" is 0.
So I think your mistake is applying the /
to the whole expression to the left. In fact as /
has a higher precedence than -
the division evaluated is 1 / 4
.
Upvotes: 3
Reputation: 34597
4 % 2 = 0 because the remainder of 4 / 2 is 0
1 / 4 is also 0 because it is doing integer division and .25 is floored to 0.
Upvotes: 5
Reputation: 182
In Python3.x it'd be 6.75 b/c 1/4 = 0.25 (true division). However, in Python 2.x 1/4 = 0 (convert to the most generic type of the arguments used, that is int in given case). Therefore, if it'd be 1. / 4 or 1 / 4. then in Python2.x you'd get 0.25 and the result would be 6.75
Upvotes: 1
Reputation: 6790
Rather than mathematics you should have a look at the operators:
a%b
is a modular b, which in case of a=4 and b=2 yields 0 (ok, thanks mathematics ;) )
1/4
: here it's important to take into account that 1 and 4 are integers. So the result will also be an integer, i.e. 0.25 becomes 0.
That's why you end up with
3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6
= 3 + 2 + 1 - 5 + 0 - 0 + 6
= 7
For operators in python have a look at the python docs.
Upvotes: 0
Reputation: 15278
See the table of operator precedences in Python.
http://docs.python.org/reference/expressions.html#summary
Upvotes: 2
Reputation: 49886
You seem to think that python should evaluate everything to the left of a /
operator before dividing by the first token to the right. That would be an odd evaluation order by any measure.
As always with programming: if you have a complex expression with infix operators, use brackets to force the correct order.
Upvotes: 1