Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

Why is it when:

>> b = -1 

>> b**2 

1

But:

>> -1**2 

-1

If i import the math library, it's no problem.

>> from math import pow 

>> pow(b,2) 

1.0 

>> pow(-1,2) 

1.0

1 Answer

0 votes
by (106k points)

The power operator binds more tightly than unary operators on its left; it binds less tightly than unary operators on its right.

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.

To know more about this you can have a look at the following video tutorial:-

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...