Just for Fun

What this chapter is about

In this frivolous chapter, I share a few practical ways I personally use Python. None of the material here is examinable, assessed, or required for 73. Think of this as a small collection of tools that might come in handy one day. There are certainly better ways to implement some of the things I show here. There are probably much better ways ; this is simply how I (a scientist pretending to be a programmer) tend to do things.

1 Downloading files using requests

This section is based on the requests module, which is a simple and widely used way of interacting with the web in Python.

import requests

The basic idea is:

  1. Send a request to a web address (URL)
  2. Receive the response
  3. Write the content to a local file
url = "https://example.com/data.txt"
local_filename = "data.txt"

response = requests.get(url)
response.raise_for_status()  # fails loudly if something goes wrong

with open(local_filename, "wb") as file:
    file.write(response.content)

Note: wb means write as binary. You may need to adjust this if the file you are downloading is a text file.

If you have many files hosted at predictable URLs, a loop is usually sufficient.

base_url = "https://example.com/files/"
filenames = ["file1.txt", "file2.txt", "file3.txt"]

for name in filenames:
    url = base_url + name
    response = requests.get(url)
    response.raise_for_status()

    with open(name, "wb") as file:
        file.write(response.content)
Account Email: chammika@nus.edu.sg
Account ID: 23911d7f-fc6d-4842-9b3c-0bd6584f9cd6

If you have many files hosted at predictable URLs, a loop is usually sufficient.


base_url="https://www.asc.ohio-state.edu/gan.1/teaching/winter10/"

for i in range(1,40):
    filename = f'Chapter{i}.pdf'             
    file_url = f'{base_url}/{filename}'
    response = requests.get(file_url)

    with open(filename,'wb') as file:
        file.write(response.content)

2 Using an API

Many websites ‘expose’ structured data through APIs (Application Programming Interfaces). An API is a way to programmatically implement actions (e.g. download a file) instead of using the buttons and text inputs at the GUI. Once set up, this can make time-consuming tasks

response = requests.get("https://api.example.com/data")
data = response.json()

print(data)

Once the data is in Python, you can analyse it, visualise it, or store it locally.

3 Animations

Python can be used to create simple animations, for example to illustrate how a quantity changes with time.

Common tools include: - matplotlib.animation - plotly - manim (for more polished explanatory animations)

Even basic animations can be surprisingly effective in lectures.

4 Interactive plots

Interactive plots allow users to zoom, pan, and inspect data points directly.

Popular libraries include: - plotly - bokeh - altair

These are particularly useful when exploring data rather than presenting final results.

5 Dashboards

Dashboards combine plots, controls, and text into a single interactive interface.

Typical Python tools: - Dash - Streamlit - Panel

They are widely used in research, industry, and data science for rapid exploration and communication of results.

Back to top