Some mathematical aspects of Python can be confusing, so Sage behaves differently from Python in several ways.
**
versus ^
. In Python, ^
means
``xor'', not exponentiation, so in Python we have
>>> 2^8 10 >>> 3^2 1 >>> 3**2 9
This use of ^
may appear odd, and it is inefficient for pure
math research, since the ``exclusive or'' function is rarely used. For
convenience, Sage pre-parses all command lines before passing them to
Python, replacing instances of ^
that are not in strings with
**
:
sage: 2^8 256 sage: 3^2 9 sage: "3^2" '3^2'
2/3
does not behave the way
mathematicians might expect. In Python, if m
and n
are ints, then m/n
is also an int, namely the quotient of m
divided by n
. Therefore 2/3=0
.
There has been talk in the Python community about changing Python so
2/3
returns the floating point number 0.6666...
, and
making 2//3
return 0
.
We deal with this in the Sage interpreter, by wrapping integer
literals in ZZ( )
and making division a constructor for
rational numbers. For example:
sage: 2/3 2/3 sage: (2/3).parent() Rational Field sage: 2//3 0 sage: int(2)/int(3) 0
L
at the end to distinguish them from
int's (and this won't change any time soon). Sage implements
arbitrary precision integers using the GMP C-library, and these print
without an L
.
Rather than modifying the Python interpreter (as some people have done for internal projects), we use the Python language exactly as is, and write a pre-parser for IPython so that the command line behavior of IPython is what a mathematician expects. This means any existing Python code can be used in Sage. However, one must still obey the standard Python rules when writing packages that will be imported into Sage.
Note:
To install a Python library, for example that you have found on
the internet, follow the directions, but run sage -python
instead of python
. Very often this means typing sage
-python setup.py install
.
See About this document... for information on suggesting changes.