Tutorial Release 10.4 The Sage Development Team https://doc.sagemath.org/pdf/en/tutorial/sage_tutorial.pdf
Sage is free, open-source math software that supports research and teaching in algebra, geometry, number theory, cryptography, numerical computation, and related areas. Both the Sage development model and the technology in Sage itself are distinguished by an extremely strong emphasis on openness, community, cooperation, and collaboration: we are building the car, not reinventing the wheel. The overall goal of Sage is to create a viable, free, open-source alternative to Maple, Mathematica, Magma, and MATLAB.
This tutorial is the best way to become familiar with Sage in only a few hours. You can read it in HTML or PDF versions, or from the Sage notebook (click Help, then click Tutorial to interactively work through the tutorial from within Sage).
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 License.
ubuntu2204
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.
Fixed indentation using SageMath:
If you fix the indentation, the function works:
Using SageMath:
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:
Using SageMath:
If you would like a single line of code to span multiple lines, use a terminating backslash:
Using SageMath:
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:
Using SageMath:
The first line below is like for(i=2;i<5;i++)
:
Using SageMath:
The third argument controls the step, so the following is like for(i=1;i<6;i+=2)
:
Using SageMath: