+ 3

Order of operations

Exponentiation has the next highest precedence, so 2**1+1 is 3, not 4, and 3*1**3 is 3, not 27. I get this from a python book and I didn't get it. Please help

19th Oct 2018, 12:40 PM
Rakib Islam
Rakib Islam - avatar
4 odpowiedzi
+ 5
The answer is in your title, its because of the order of the mathematical operators. it will first do 1**3 which equals 1 and then do 1 *3 which is 3. So the square operator goes before the multiply operator.
19th Oct 2018, 1:40 PM
Willem Roos
+ 2
Its like in math.. In arithmetics we learned that multiplications and divisions have precedence respect addition/subtractction at example: 2 + 3 * 2 * 1= 2 + ((3*2)*1) = 8 Same with python (and all programming language) that have own rules for precedence. In your case: 2**1+1 = (2**1)+1= 3 3*1**3= 3 * (1**3) = 3*1= 3 becuse exponentation operator (**) have higher precendence over addition and multiplication
19th Oct 2018, 1:40 PM
KrOW
KrOW - avatar
+ 1
It means first exponential will be evaluated then other operation like addition subtraction is perform by your example 2**1+1 in this first exponent will evaluate so 2**1 which is 2then it will add to 1 so 2+1=3 so 3 is ans. So as 3*1**3 first 1**3=3 then 3*1=3 so ans. Is 3
19th Oct 2018, 1:40 PM
MsJ
MsJ - avatar
+ 1
Thanks a lot. Now I get it
19th Oct 2018, 3:43 PM
Rakib Islam
Rakib Islam - avatar