+ 6

Why this happened?

print(-10**2) » output is -100 n=-10 print(n**2) » output is 100.

12th Jul 2023, 10:52 AM
ⵢⴷⵢⴷ
ⵢⴷⵢⴷ - avatar
15 odpowiedzi
+ 17
Lisa is spot on with her answer The first example -> print(-10**2) is interpreted as - ( 10**2) -> -100 The 2nd example -> n=-10 print(n**2) is interpreted as (-10)**2 -> 100 n = -10 is applied before the exponentation
12th Jul 2023, 12:03 PM
Rik Wittkopp
Rik Wittkopp - avatar
+ 10
"**" has higher precedence than "-", so "**" is considered first.
12th Jul 2023, 11:34 AM
Lisa
Lisa - avatar
+ 6
Wow bro, it's really confusing how this happens!
12th Jul 2023, 11:14 AM
Sancho Godinho
Sancho Godinho - avatar
+ 4
Yabtsega Alem Create an own thread for your question, do not hijack other people's thread
13th Jul 2023, 7:31 AM
Lisa
Lisa - avatar
+ 4
ⵢⴷⵢⴷ Review your understanding of mathematical operator precedence. This is a math problem, not a code problem
13th Jul 2023, 10:49 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
ⵢⴷⵢⴷ I am sure that you recognise that 10 is different to -10 n is a variable which has a value of -10 This value is then passed to the equation **2 -10 * -10 = 100 The equation of -10**2 is subject to operator precedence. ** has a higher precedence than - so the equation operates on 10 * 10 = 100 Then it uses the - to make -100
13th Jul 2023, 10:33 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
Thanks you Wittkopp but I will need more time to understanding it more.
13th Jul 2023, 10:45 AM
ⵢⴷⵢⴷ
ⵢⴷⵢⴷ - avatar
+ 2
Yeah, 😅 that's a bad news for me because I'm not good in Math.
13th Jul 2023, 12:09 PM
ⵢⴷⵢⴷ
ⵢⴷⵢⴷ - avatar
+ 2
Ashly Mathew Create your own thread for asking your question. Do not hijack other people's thread.
14th Jul 2023, 9:18 AM
Lisa
Lisa - avatar
+ 1
Python uses () around operators not variabels.
12th Jul 2023, 11:19 AM
Anton Drud
Anton Drud - avatar
+ 1
The math rules are that, - * - = + Example: -4 * -6 = 24, so -10 * -10 = 100 That's what I learned at school.
12th Jul 2023, 12:36 PM
Jan
Jan - avatar
+ 1
print(-10**2)>>>> this is like this firstly it will Calculate the (10**2) and then that small negative sign will be added to the number So in your next Part its like this : n=-10 print(n**2) >>>>>>>>> print( (n)**10) >>> print((-10)**2) so the output will be 100
13th Jul 2023, 10:03 AM
Shahab MirHosseini
Shahab MirHosseini - avatar
+ 1
Why the same values with the same operation result in different outcome? Is there difference between an assigned negative value and a non-assigned negative value ?
13th Jul 2023, 10:14 AM
ⵢⴷⵢⴷ
ⵢⴷⵢⴷ - avatar
0
i am pretty sure it is Python.
12th Jul 2023, 10:59 AM
Anton Drud
Anton Drud - avatar
0
It is because python automaticly sets () around **
12th Jul 2023, 11:13 AM
Anton Drud
Anton Drud - avatar