0
Number addition
Print(5/2+5/2) Answer is 5?
2 Answers
+ 5
In Python 3 (which is the most common version now), the / operator performs float division. So, 5/2 evaluates to 2.5 thus 2.5 + 2.5 is equal to 5.0
However if you used // this would have produced an integer division of 5//2 as 2 then 2 + 2 = 4
+ 3
Muhammad Rehman were you expecting 3.75?
That would be the result if the expression were evaluated strictly left-to-right ((5/2) + 5)/2. But it does not work that way. Just like standard mathematics, some operators have higher priority over others. In this case, divisions happen first, and then addition. So it becomes ((5/2) + (5/2)).
Starting with highest priority, the order of evaluation for mathematical operators is this:
() Parentheses
** Exponents
+, - Unary operators (positive or negative signs)
*, /, // Multiplication and Division
+, - Addition and Subtraction
Some people learned this principle as an acronym:
PEMDAS: Parentheses, Exponents, Multiplication, Addition, Subtraction
Or as a mnemonic:
Parachute Expert My Dear Aunt Sally