Python for analysis

Use Python to support the delicate parts of real analysis: limits, sequences, series, and numerical integrals. The aim is to see theorems and examples move in front of you.

This page assumes you are comfortable with the basics from Python Lab • Foundations. You should already know how to define functions, loop over values, and print results.

You can run all the code below in the main Python Lab pad or in your own Colab / Jupyter notebook.

Unit 1 • Sequences and numeric limits

In class, you work with limits like lim a_n as n → ∞. Python lets you generate many terms quickly and see what they appear to approach.

1.1 — A classical sequence: (1 + 1/n)^n

def a(n):
    return (1 + 1/n)**n

Ns = [1, 2, 5, 10, 50, 100, 1000, 10000]
for n in Ns:
    print(n, a(n))
  • • Compare the values with the number 2.718281828....
  • • Change the definition to (1 + 1/n)**(2*n) and run again.
  • • Try other sequences from your real analysis course.

1.2 — Oscillating sequences

Some sequences do not settle at a single value. Python helps you see that behaviour clearly:

def b(n):
    return (-1)**n

for n in range(1, 11):
    print(n, b(n))

Extend this idea to sequences like (-1)^n / n and compare how they behave.

Unit 2 • Series and partial sums

A computer cannot sum infinitely many terms, but it can compute partial sums quickly. This is often enough to build intuition before you apply convergence tests.

2.1 — The p-series ∑ 1/n²

def partial_sums_p2(N):
    s = 0.0
    for n in range(1, N+1):
        s += 1/n**2
    return s

for N in [1, 2, 3, 5, 10, 20, 50, 100, 500, 1000]:
    print(N, partial_sums_p2(N))
  • • Compare the partial sums with the known value π²/6.
  • • Discuss how many terms you need to get 2 decimal places correct.

2.2 — Contrasting with the harmonic series

def partial_sums_harmonic(N):
    s = 0.0
    for n in range(1, N+1):
        s += 1/n
    return s

for N in [1, 2, 3, 5, 10, 20, 50, 100, 500, 1000, 5000]:
    print(N, partial_sums_harmonic(N))

Watch how the harmonic series grows slowly but never settles. Use this numeric behaviour as support for proofs you see in class.

2.3 — Alternating series

def partial_sums_alt(N):
    s = 0.0
    for n in range(1, N+1):
        s += ((-1)**(n+1)) / n
    return s

for N in [1, 2, 3, 5, 10, 20, 50, 100]:
    print(N, partial_sums_alt(N))

This illustrates how alternating signs can affect convergence. You can compare this with the harmonic series above and link it to the alternating series test.

Unit 3 • Numerical integrals (Riemann-style)

In calculus, you define integrals using limits of Riemann sums. Python lets you work with the approximations directly. The goal is not to replace theory, but to build strong intuition.

3.1 — Midpoint rule for a single integral

import math

def approx_integral(f, a, b, n):
    h = (b - a) / n
    s = 0.0
    for k in range(n):
        x = a + (k + 0.5) * h  # midpoint
        s += f(x)
    return s * h

print("Approximate ∫_0^π sin(x) dx")
for n in [10, 50, 100, 500, 1000]:
    print("n =", n, "approx =", approx_integral(math.sin, 0, math.pi, n))
  • • Compare the approximations with the exact value 2.
  • • Observe how the error changes as n increases.
  • • Try other functions: math.exp, 1/(1+x**2), etc.

3.2 — Small lab task for students

Choose a definite integral from your course (with known exact value if possible). For example:

  • • ∫₀¹ 1/(1+x²) dx
  • • ∫₀¹ x² dx
  • • ∫₀¹ eˣ dx

Translate the integrand into a Python function and use approx_integral to approximate it for several values of n. Then compare your numeric results with the exact answer from calculus.

For tutors and lecturers

You can plug this page into your courses in very specific ways:

  • Real analysis I: Ask students to bring numeric experiments for one limit and one series each week.
  • Sequences and series: Use Unit 2 to illustrate “convergence vs divergence” before stating tests.
  • Calculus II: Use Unit 3 to connect Riemann sums to concrete numerical approximations.

Students can include short Python outputs in their writeups or present their findings during NAMSSN events and problem sessions.

Where to go next