### Thread: Why doesn't the '**' operator return a positive value

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2013
Posts
4
Rep Power
0

#### Why doesn't the '**' operator return a positive value

Hi everybody

I have recently started learning python from C++ and its been great! However I ran into a bug which was a bit annoying but I soon found out what it was. When I was using comparative operators for conditional statements, I realised that the '**' operator returns a negative value when I write an equation like this [negative number]**2. I don't understand this because surely it would return a postive number as it is doing say -5 * -5 which would return 25? Is there any other way to make multiply a base to the power of x to make it return a positive value in the given circumstance? Thank you!
2. It has to do with python's operator precedence.
x = (-5) ** 2 sets x to 25
x = -5 ** 2 sets x to -25

This is because, without the parenthesis, it will evaluate 5 ** 2 first and then put the - sign on the result and hence you get -25.
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2013
Posts
4
Rep Power
0
Originally Posted by Scorpions4ever
It has to do with python's operator precedence.
x = (-5) ** 2 sets x to 25
x = -5 ** 2 sets x to -25

This is because, without the parenthesis, it will evaluate 5 ** 2 first and then put the - sign on the result and hence you get -25.
Thank you, this makes sense now!
4. This works too ...
Code:
```a = -5
x = a**2
print(x)  # 25```