---
author: Nathan Carter (ncarter@bentley.edu)
---

This answer assumes you have imported SymPy as follows.


```python
from sympy import *                   # load all math functions
init_printing( use_latex='mathjax' )  # use pretty math output
```

Sequences are typically written in terms of an independent variable $n$,
so we will tell SymPy to use $n$ as a symbol, then define our sequence
in terms of $n$.

We define a term of an example sequence as $a_n=\frac{1}{n+1}$, then
build a sequence from that term.  The code `(n,0,oo)` means that $n$
starts counting at $n=0$ and goes on forever (with `oo` being the SymPy
notation for $\infty$).


```python
var( 'n' )                       # use n as a symbol
a_n = 1 / ( n + 1 )              # formula for a term
seq = sequence( a_n, (n,0,oo) )  # build the sequence
seq
```




$\displaystyle \left[1, \frac{1}{2}, \frac{1}{3}, \frac{1}{4}, \ldots\right]$



You can ask for specific terms in the sequence, or many terms in a row, as follows.


```python
seq[20]
```




$\displaystyle \frac{1}{21}$




```python
seq[:10]
```




$\displaystyle \left[ 1, \  \frac{1}{2}, \  \frac{1}{3}, \  \frac{1}{4}, \  \frac{1}{5}, \  \frac{1}{6}, \  \frac{1}{7}, \  \frac{1}{8}, \  \frac{1}{9}, \  \frac{1}{10}\right]$



You can compute the limit of a sequence,

$$ \lim_{n\to\infty} a_n. $$


```python
limit( a_n, n, oo )
```




$\displaystyle 0$



55b5054f556e227c9801fc25b6839e806570a76e
ipynb
Jupyter Notebook
database/tasks/How to define a mathematical sequence/Python, using SymPy.ipynb
nathancarter/how2data
7d4f2838661f7ce98deb1b8081470cec5671b03a
MIT
database/tasks/How to define a mathematical sequence/Python, using SymPy.ipynb
nathancarter/how2data
7d4f2838661f7ce98deb1b8081470cec5671b03a
MIT
database/tasks/How to define a mathematical sequence/Python, using SymPy.ipynb
nathancarter/how2data
7d4f2838661f7ce98deb1b8081470cec5671b03a
MIT
2021-07-18T19:01:29.000Z
2022-03-29T06:47:11.000Z
Qwen/Qwen-72B
1. YES
2. YES