With some minor exceptions, Sage uses the Python programming language, so most introductory books on Python will help you to learn Sage.
Sage uses =
for assignment. It uses ==,<=,>=,<
and >
for comparison:
sage: a = 5 sage: a 5 sage: 2 == 2 True sage: 2 == 3 False sage: 2 < 3 True sage: a == 5 True
Sage provides all of the basic mathematical operations:
sage: 2**3 # ** means exponent 8 sage: 2^3 # ^ is a synonym for ** (unlike in Python) 8 sage: 10 % 3 # for integer arguments, % means mod, i.e., remainder 1 sage: 10/4 5/2 sage: 10//4 # for integer arguments, // returns the integer quotient 2 sage: 4 * (10 // 4) + 10 % 4 == 10 True sage: 3^2*4 + 2%5 38
The computation of an expression like 3^2*4 + 2%5
depends on
the order in which the operations are applied; this is specified in
the ``operator precedence table'' in Section A.1.
Sage also provides many familiar mathematical functions; here are just a few examples:
sage: sin(pi/3) sqrt(3)/2 sage: exp(2) e^2 sage: n(exp(2)) # n(-) returns a numerical approximation to its argument 7.38905609893065 sage: erf(1.4) 0.952285119762649
Python is dynamically typed, so the value referred to by each variable has a type associated with it, but a given variable may hold values of any Python type within a given scope:
sage: a = 5 # a is an integer sage: type(a) <type 'sage.rings.integer.Integer'> sage: a = 5/3 # now a is a rational number sage: type(a) <type 'sage.rings.rational.Rational'> sage: a = 'hello' # now a is a string sage: type(a) <type 'str'>
A potential source of confusion in Python is that an integer literal that begins with a zero is treated as an octal number, i.e., a number in base 8.
sage: 011 9 sage: 8 + 1 9 sage: n = 011 sage: n.str(8) # string representation of n in base 8 '11'
See About this document... for information on suggesting changes.