Getting Help
Sage has extensive built-in documentation, accessible by typing the name of a function or a constant (for example), followed by a question mark:
Sage also provides ‘Tab completion’: type the first few letters of a function and then hit the tab key. For example, if you type "ta" followed by "TAB", Sage will print "tachyon, tan, tanh, taylor". This provides a good way to find the names of functions and other structures in Sage.
Functions, Indentation, and Counting
To define a new function in Sage, use the "def" command and a colon after the list of variable names. For example:
Note: Depending on which version of the tutorial you are viewing, you may see three dots "....:" on the second line of this example. Do not type them; they are just to emphasize that the code is indented. Whenever this is the case, press [Return/Enter] once at the end of the block to insert a blank line and conclude the function definition.
You do not specify the types of any of the input arguments. You can specify multiple inputs, each of which may have an optional default value. For example, the function below defaults to "divisor=2" if "divisor" is not specified.
You can also explicitly specify one or either of the inputs when calling the function; if you specify the inputs explicitly, you can give them in any order:
In Python, blocks of code are not indicated by curly braces or begin and end blocks as in many other languages. Instead, blocks of code are indicated by indentation, which must match up exactly. For example, the following is a syntax error because the "return" statement is not indented the same amount as the other lines above it.
If you fix the indentation, the function works:
Semicolons are not needed at the ends of lines; a line is in most cases ended by a newline. However, you can put multiple statements on one line, separated by semicolons:
If you would like a single line of code to span multiple lines, use a terminating backslash:
In Sage, you count by iterating over a range of integers. For example, the first line below is exactly like "for(i=0; i<3; i++)" in C++ or Java:
The first line below is like "for(i=2;i<5;i++)".
The third argument controls the step, so the following is like "for(i=1;i<6;i+=2)".
Often you will want to create a nice table to display numbers you have computed using Sage. One easy way to do this is to use string formatting. Below, we create three columns each of width exactly 6 and make a table of squares and cubes.
The most basic data structure in Sage is the list, which is – as the name suggests – just a list of arbitrary objects. For example, the "range" command that we used creates a list (in python 2):
Here is a more complicated list:
List indexing is 0-based, as in many programming languages.
Use "len(v)" to get the length of "v", use "v.append(obj)" to append a new object to the end of "v", and use "del v[i]" to delete the entry of "v":
Another important data structure is the dictionary (or associative array). This works like a list, except that it can be indexed with almost any object (the indices must be immutable):
You can also define new data types using classes. Encapsulating mathematical objects with classes is a powerful technique that can help to simplify and organize your Sage programs. Below, we define a class that represents the list of even positive integers up to n; it derives from the builtin type "list".
The "init" method is called to initialize the object when it is created; the "repr" method prints the object out. We call the list constructor method in the second line of the "init" method. We create an object of class "Evens" as follows:
Note that "e" prints using the "repr" method that we defined. To see the underlying list of numbers, use the "list" function:
We can also access the "n" attribute or treat "e" like a list.