为什么Python`**`用于取幂而不是`^`运算符? [英] Why does Python `**` use for exponentiation instead of the `^` operator?

查看:57
本文介绍了为什么Python`**`用于取幂而不是`^`运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 ^ 在Python中不平方?我知道取幂是 ** ,但是 ^ 到底是什么,为什么不使用该运算符呢?

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

例如 2 ^ 2 = 0 3 ^ 2 = 1 .

推荐答案

^ 运算符已用于按位异或.

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

这使旧的 Fortran风格 ** 求幂运算符.

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

>>> base = 5
>>> exp = 2
>>> base ** exp
25

这篇关于为什么Python`**`用于取幂而不是`^`运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆