Sage/Crash Course

Understanding Objects edit

To work with Sage you have to know a little bit of Python, because it is the underlying language Sage is written in and it is the language Sage understands!

  • Objects: An object is a data structure with methods that operate on the data inside an object. In Python - and Sage - everything is an object, even the number "1".
  • You can store objects in variables. That is a string of characters where only a-z, A-Z, digits (digits must not be at the first position) and the sign "_" is allowed (e.g. "abs_1"). To accomplish this, enter "x=1" and indicate with "Shift-Return/Enter" that you have ended your input.
  • To access the data (the object's value) behind the variable, you have to enter it and probably write "print" in front of it.
  • The methods of an object can be accessed via the "." sign and then comes the name and parenthesis "(...)", where they can be empty or have arguments.
  • The data stored inside an object is accessed similarly just by the "." and the name but with no parenthesis.
  • Functions are objects that do not need an object.

Example:

# storing the object "11" in "x"
sage: x = 11

#show me "x"
sage: print x
11

# factorial() is a method of x
sage: x.factorial()
39916800

# sqrt is a function
sage: sqrt(x)
sqrt(11)
  • Objects can be combined with others:
    • using an operator sign like "+", "*", ...
    • inserting them into the argument list of a method or function

Example:

# operator "+" adds the values of "x" and "x"
sage: x+x
22

# function "n" evaluates the numerical value of "sqrt(11)"
sage: n(sqrt(11), digits=50)
3.3166247903553998491149327366706866839270885455894