Home > database >  Python ** for negative numbers
Python ** for negative numbers

Time:07-13

I am new to python. ** => operator is used for calculating exponential .

print(2 ** 3)  //8
print(-2 ** 3) //-8
print(-2 ** 2) //-4

It should print 4 only right ?

CodePudding user response:

See 6.5. The power operator. And more specifically:

Thus, in an unparenthesized sequence of power and unary operators, the operators are evaluated from right to left (this does not constrain the evaluation order for the operands): -1**2 results in -1.

CodePudding user response:

Instead of

print(-2 ** 2)

use this code

print(int(-2) ** 2)
  • Related