sage笔记本中的操作符与从Python导入

时间:2014-12-27 02:27:38

标签: python sage

运算符的行为会有所不同,具体取决于我运行sage程序的方式。 在笔记本中:

2^10 
==>1024

使用sage -python filename.py运行我的程序:

from sage.all import *
print(2^10)
==> 8

我需要在Python中导入什么来复制Sage笔记本的行为?

编辑:

感谢大家参加基础Python课程。帝斯曼回答了这个问题 在评论中,sage笔记本有一个预处理器。

1 个答案:

答案 0 :(得分:2)

Exponentiation的python中,我们使用双星号**

>>> print (2**10)
1024

或者您可以使用内置功能 pow

>>> pow(2, 10)
1024

<强> POW

pow(...)
    pow(x, y[, z]) -> number

    With two arguments, equivalent to x**y.  With three arguments,
    equivalent to (x**y) % z, but may be more efficient (e.g. for longs).
enter code here

^是执行XOR(bitwise exclusive or)操作的按位运算符。

例如

>>> a = [1,2,3]
>>> b = [3,4,5]
>>> a^b
>>> set(a)^set(b)
set([1, 2, 4, 5])

<强> x ^ y

Does a "bitwise exclusive or". 
Each bit of the output is the same as the corresponding bit in x if that bit in y is 0, 
and it's the complement of the bit in x if that bit in y is 1.

Just remember about that infinite series of 1 bits in a negative number, and these 
should all make sense.