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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With