Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for Blaec (CSO) Tasks and Strategy Outline.
Download

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.

https://creativecommons.org/licenses/by-sa/3.0/

386 views
ubuntu2204
Kernel: SageMath 10.4

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.

def even(n): v = [] for i in range(3, n): if i % 2 == 0: v.append(i) return v

Fixed indentation using SageMath:

from sage.all import * def even(n): v = [] for i in range(Integer(3), n): if i % Integer(2) == Integer(0): v.append(i) return v even(Integer(10))
[4, 6, 8]

If you fix the indentation, the function works:

def even(n): v = [] for i in range(3, n): if i % 2 == 0: v.append(i) return v even(10)
[4, 6, 8]
[4, 6, 8]
[4, 6, 8]

Using SageMath:

from sage.all import * def even(n): v = [] for i in range(Integer(3), n): if i % Integer(2) == Integer(0): v.append(i) return v even(Integer(10))
[4, 6, 8]
[4, 6, 8]
[4, 6, 8]

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:

sage: a = 5; b = a + 3; c = b^2; c
64
64
64

Using SageMath:

from sage.all import * a = Integer(5); b = a + Integer(3); c = b**Integer(2); c
64
64
64

If you would like a single line of code to span multiple lines, use a terminating backslash:

2 + \ 3
5
5
5

Using SageMath:

from sage.all import * Integer(2) + Integer(3)
5
5
5

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:

for i in range(3): print(i)
0 1 2
0 1 2

Using SageMath:

from sage.all import * for i in range(Integer(3)): print(i)
0 1 2

The first line below is like for(i=2;i<5;i++):

sage: for i in range(2, 5): ....: print(i)
2 3 4

Using SageMath:

from sage.all import * for i in range(Integer(2), Integer(5)): print(i)
2 3 4

The third argument controls the step, so the following is like for(i=1;i<6;i+=2):

sage: for i in range(1, 6, 2): ....: print(i)
1 3 5

Using SageMath:

from sage.all import * for i in range(Integer(1), Integer(6), Integer(2)): print(i)
1 3 5