SageMath Lab • Foundations

A slow, gentle introduction to SageMath as a notebook environment: where to run it, how cells behave, and how to think about Sage objects before jumping into heavier algebra.

1. First contact with SageMath

SageMath is most comfortable inside a notebook. Each notebook is made of cells. You write code in a cell, press “Run”, and the output appears just underneath. You can then adjust the code and run it again until it behaves the way you want.

Where can I run Sage?
• The live cell on the main SageMath Lab page (quick experiments).
• A public Sage server (for example sagecell.sagemath.org).
• A local Sage installation on your own laptop (recommended for heavy work).

Your very first cell does not need to be grand. A better goal is simply: “I can press Run and read the output calmly.”

First lines to test the notebook
# Simple arithmetic
2 + 3
2^10
(3^5 - 1) / 2

# A first factorisation
factor(2^16 - 1)

# A tiny loop (watch the indentation)
for n in range(2, 8):
    print(n, n^2 + n + 1)
Paste this into the Sage scratchpad on the main lab page, then send it to the live cell.

When this runs without errors, you have taken the first step: the environment is alive, and you can begin to treat it like a partner in your calculations.

2. Sage integers, rationals, and the idea of “objects”

Sage is built on Python, but it adds mathematical objects such as integer rings, rational fields, and symbolic expressions. Instead of guessing types, you can ask Sage exactly what you are working with.

Sage integers and rationals (ZZ and QQ)
# Compare plain Python and Sage integers
n_python = 5           # plain Python int
n_sage   = ZZ(5)       # Sage integer in ZZ

print(n_python, type(n_python))
print(n_sage,   type(n_sage))

# Exact rational arithmetic
r1 = QQ(1) / 3
r2 = QQ(2) / 7
print("r1 =", r1)
print("r2 =", r2)
print("r1 + r2 =", r1 + r2)

# Ask Sage what kind of object r1 is
print("Parent of r1:", r1.parent())
Running this once gives a good mental picture of how Sage is tracking arithmetic objects behind the scenes.

The vague idea to keep in mind is: Sage cares about where numbers live. The ring or field (“parent”) matters for what operations make sense.

Habit: whenever you get confused by a result, print type(x) or x.parent(). It is often the fastest way to understand what is going on.

3. First symbolic variables and plots

Sage can manipulate expressions symbolically. To do this, you declare your variables first and then build expressions out of them.

📈 Declaring variables and plotting
# Declare x as a symbolic variable
var('x')

# Define a symbolic expression
f = sin(x) / x
print("f(x) =", f)

# Plot it on an interval (works in a full Sage notebook)
p = plot(f, (x, -10, 10))
p.show()

# Multiple curves on one plot
q = plot([sin(x), cos(x)], (x, 0, 2*pi))
q.show()
The plots will appear directly under the cell in a real Sage notebook (local or online).

In the public SageCell (and in the LaTeX–like styling of your notebook), these plots give immediate feedback for calculus and analysis questions.

4. Organising your Sage workspace

Instead of one giant “playground” notebook, it is better to keep a small library of focused notebooks. This mirrors how real research is done: separate files, clear names, saved experiments.

Suggested naming style

  • alg-2025-s1-primes.sage — experiments for an algebra course segment.
  • nt-project-congruences.sage — a mini project on congruences.
  • lin-alg-determinants-ideas.sage — determinant patterns for a linear algebra course.

On the main SageMath Lab page, the Sage scratchpad is designed as a temporary place to shape snippets. Once a snippet feels stable, paste it into a proper notebook file and add a short text cell explaining what it does.

Code copied to clipboard