+ 6

# Why this happened?

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

15 ответов

+ 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

+ 10

"**" has higher precedence than "-", so "**" is considered first.

+ 6

Wow bro, it's really confusing how this happens!

+ 4

Yabtsega Alem Create an own thread for your question, do not hijack other people's thread

+ 4

ⵢⴷⵢⴷ
Review your understanding of mathematical operator precedence.
This is a math problem, not a code problem

+ 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

+ 2

Thanks you Wittkopp but I will need more time to understanding it more.

+ 2

Yeah, 😅 that's a bad news for me because I'm not good in Math.

+ 2

Ashly Mathew
Create your own thread for asking your question. Do not hijack other people's thread.

+ 1

Python uses () around operators not variabels.

+ 1

The math rules are that, - * - = +
Example: -4 * -6 = 24, so -10 * -10 = 100
That's what I learned at school.

+ 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

+ 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 ?

0

i am pretty sure it is Python.