Cheat sheet: Python, SymPy, SageMath#
This page collects the commands used most often in the lectures and labs. Annotated examples and explanations of what happens “under the hood” can be found in the chapters Python for mathematicians: first steps and How does a computer algebra system work?.
Python#
Task |
Code |
|---|---|
power, integer quotient, remainder |
|
exact fractions |
|
GCD, LCM, integer square root \(\lfloor\sqrt{n}\rfloor\) |
|
binomial coefficient, factorial |
|
comparing |
|
list comprehension |
|
quantifiers |
|
sum, product |
|
Cartesian product, combinations |
|
memoization (caching results) |
|
Warning
In Python, ^ is XOR, not exponentiation. 0.1 + 0.2 != 0.3. round(2.5) == 2, because Python rounds halves to the nearest even integer.
SymPy#
import sympy as sp
x, y = sp.symbols('x y') # symbole
n = sp.symbols('n', integer=True, positive=True) # symbol z założeniami
Task |
Code |
|---|---|
rational number, square root |
|
approximation to a given precision |
|
expansion, factorization |
|
rational functions |
|
simplification |
|
substitution |
|
equations |
|
sum, limit, derivative, integral |
|
recurrences |
|
expression tree |
|
numerical function |
|
plot |
|
SageMath (for comparison)#
Task |
SageMath |
|---|---|
power, fraction |
|
symbolic variable |
|
polynomial ring |
|
factorization |
|
number field |
|
matrix |
|
range |
|
approximation |
|