Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Using batteries included with Python

Open In Colab

In the previous section, you learned how to write and import your own custom scripts. While building your own tools is a vital skill, you rarely need to invent everything from scratch.

When you install Python, it automatically comes pre-packaged with the Python Standard Library. This is a substantial collection of highly optimized, ready-to-use modules. Whether you need to run complex statistical math, generate random coordinate samples, or navigate your computer’s file system, the tools are already sitting on your machine. You do not need to download or install anything extra.

You just have to unlock them using the import command.


1. Importing modules

To use a module from the Standard Library, you must import it at the top of your script or notebook. Once imported, you can access all the functions inside it by typing the module name, a dot, and the function name.

import math

# Use the square root function from the math module
result = math.sqrt(25)
print(result)

If you only need a specific tool, you can import just that part. This allows you to use the tool directly without typing the module name every time.

from math import pi

print(f"The value of pi is approximately {pi:.44f}")

2. Advanced math and statistics

The math module provides access to advanced mathematical functions and constants. A great spatial data science example is calculating the exact distance between two coordinates on the spherical Earth using the Haversine formula.

Diagram comparing great-circle distance on a sphere's surface to a straight line cutting through it.

The Haversine formula calculates the shortest distance between two points across the spherical surface of the Earth (great-circle distance), rather than a straight line through it.

import math

san_francisco = (37.77, -122.41)
new_york = (40.66, -73.94)


def haversine_distance(origin, destination):
    """Calculates spherical distance between two coordinates in meters."""
    lat1, lon1 = origin
    lat2, lon2 = destination
    radius = 6371000  # Earth radius in meters

    dlat = math.radians(lat2 - lat1)
    dlon = math.radians(lon2 - lon1)

    a = (
        math.sin(dlat / 2) ** 2
        + math.cos(math.radians(lat1))
        * math.cos(math.radians(lat2))
        * math.sin(dlon / 2) ** 2
    )
    c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))

    return radius * c


dist = haversine_distance(san_francisco, new_york)
print(f"Distance: {dist / 1000:.1f} km")

If you need to analyze lists of numbers, the statistics module is incredibly useful. It provides functions for calculating the mean, median, and mode of your data without writing loops yourself.

import statistics

elevations = [450, 1200, 890, 2300, 1200, 500]

print(f"Average: {statistics.mean(elevations)} m")
print(f"Median: {statistics.median(elevations)} m")

Concept check

Predict the result of these two common rounding operations. Remember to think about what “up” and “down” mean on a number line.

import math

# Predict both results
up_round = math.ceil(-5.2)
down_round = math.floor(-5.2)

3. Adding unpredictability

The random module is used to generate random numbers and make random selections. This is especially useful in spatial sampling, simulations, or creating dummy data for testing.

import random

# Pick a random integer between 1 and 10
random_number = random.randint(1, 10)
print(f"Random number: {random_number}")

# Pick a random item from a list
cities = ["Zurich", "Geneva", "Basel", "Bern"]
random_city = random.choice(cities)
print(f"Randomly selected city: {random_city}")

Concept check

We need to pick two test sites. Predict which method guarantees you will select two unique cities.

import random
cities = ["Zurich", "Geneva", "Basel", "Bern"]

# Method A (requires two function calls)
site_1 = random.choice(cities)
site_2 = random.choice(cities)

# Method B (requires one function call)
sites_group = random.sample(cities, k=2)

4. Navigating your computer

When working with local data, you often need to know exactly which folder your Python code is running in, or you need to build paths to your data files. The os module (Operating System) and the pathlib module help you interact with your computer’s file system.

While os is the older, traditional way to handle paths using text strings, modern Python developers prefer pathlib because it treats paths as smart objects that automatically handle the messy differences between Windows and Mac/Linux folders.

A directory tree diagram showing how pathlib joins folder names to reach a target file.

Pathlib allows you to navigate directory trees programmatically, intelligently joining parent folders to file names using the / operator.

import os
from pathlib import Path

# Using OS to get the current working directory
current_folder_os = os.getcwd()
print(f"OS Path: {current_folder_os}")

# Using Pathlib to do the same thing, but creating a smart Path object
current_folder_pathlib = Path.cwd()
print(f"Pathlib Object: {current_folder_pathlib}")

# Pathlib makes joining folders to file names incredibly easy using the / operator
data_file = current_folder_pathlib / "data" / "elevations.csv"
print(f"Looking for data at: {data_file}")

Concept check

Examine how the / operator works. Predict the type and content of the final_path variable.

from pathlib import Path

# Assume our current folder is: C:/project/data/
my_folder = Path.cwd()

# The division operator works on smart objects!
final_path = my_folder.parent / "output" / "map.tif"

5. Formatting data with JSON

JSON (JavaScript Object Notation) is a lightweight text format used to store and transport data. It looks almost exactly like Python dictionaries and lists. The json module allows you to convert text data into usable Python objects.

This is a critical skill for working with Web APIs, which almost always return data in JSON format.

A diagram showing a raw text string transforming into a structured Python dictionary.

The json.loads() function translates a flat string of text into a structured, hierarchical Python dictionary that you can navigate using keys.

import json

# A string of text formatted as JSON
api_response_text = (
    '{"city": "Zurich", "coordinates": [47.37, 8.54], "population": 415000}'
)

# Convert the text string into a real Python dictionary
data = json.loads(api_response_text)

# Now we can access it using standard dictionary keys
print(data["city"])
print(data["coordinates"])

Concept check

You have successfully used json.loads() to convert an API response into a Python dictionary named data.

Examine the raw JSON structure. Predict the nested key access required to extract the longitude (the first value in the coordinates list).

{
  "properties": {
    "name": "Eiffel Tower",
    "location": {
      "city": "Paris",
      "coordinates": [2.29, 48.85]
    }
    ...
  }
}
# Predict the correct indexing path here. Fill in the blanks:
# Hint: You will need multiple sets of []
longitude_val = data[...][...][...][...]

6. Exercises

To practice using the Standard Library, complete the following tasks.


Exercise 1: Random coordinates

Using the random module, write a loop that generates 3 random coordinate pairs (representing latitude and longitude). Assume the latitude must be a decimal between 45.0 and 47.0, and the longitude must be a decimal between 6.0 and 9.0. Print each pair rounded to 4 decimal places.

# Write your code here
An infographic scale showing how geographic coordinate precision relates to physical sizes like meters, millimeters, and nanometers.

The relationship between the number of decimal places in a geographic coordinate and the physical distance it represents on the ground.


Exercise 2: Parsing JSON

You downloaded a small JSON string representing a feature collection. Use the json module to parse it into a Python dictionary, then print the name of the feature.

geojson_string = '{"type": "Feature", "properties": {"name": "Matterhorn"}, "geometry": {"type": "Point", "coordinates": [7.65, 45.97]}}'

7. Summary

In this section, you learned how to leverage the Python Standard Library to solve common programming tasks without installing external tools. You discovered how to:

What comes next?

The Standard Library is powerful, but it has its limits. What if you need to calculate highly accurate spatial distances across the Earth’s curved surface, or add visual progress bars to a massive loop?

Instead of writing that complex code from scratch, you can tap into the massive open-source community. In the next section, Third Party Modules, we will explore how to find, install, and use external packages to extend Python’s capabilities far beyond its built-in tools.