Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Python `**` use for exponentiation instead of the `^` operator? [duplicate]

Why is ^ not squaring in Python? I know exponentiation is ** instead, but what exactly is ^ and why wasn't that operator used instead?

For example 2^2=0, 3^2=1.

like image 534
ZHU Avatar asked Jun 24 '26 02:06

ZHU


1 Answers

The ^ operator was already used for bitwise xor.

>>> x = 42; format(x, '08b')
'00101010'
>>> y = 137; format(y, '08b')
'10001001'
>>> z = x ^ y; format(z, '08b')
'10100011'

That leaves the old Fortran-style ** operator for exponentiation.

>>> base = 5
>>> exp = 2
>>> base ** exp
25
like image 184
Raymond Hettinger Avatar answered Jun 26 '26 15:06

Raymond Hettinger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!