All posts
Python17 February 2026·4 min read

Python Essentials for Machine Learning

A quick-start guide to Python data structures and libraries you'll use constantly in ML — tuples, dictionaries, sets, and essential imports.

Why Python?

Python is the lingua franca of machine learning. Its readable syntax and massive library ecosystem make it the go-to language for data science. As a designer learning ML, you don't need to become a software engineer — but knowing the basics will let you prototype, explore data, and speak the same language as your engineering team.

Core Data Structures

Tuples — Immutable Sequences

A tuple is like a list that can't be changed after creation. Defined with parentheses:

coordinates = (42.3601, -71.0589)
land_cover = ("forest", "urban", "water")

Key behaviours:

  • Immutable — you can't update existing elements
  • Slicing uses start:stop (stop is excluded)
  • Exception: if a list lives inside a tuple, the list's contents can be modified

Tuple unpacking lets you assign each element to a variable in one line:

field_data = ("Auckland", "residential", 2024, "clean")
city, land_use, year, status = field_data

# Use _ to skip values you don't need
city, _, _, status = field_data

Dictionaries — Key-Value Pairs

Dictionaries store data as key-value pairs. They're how you'll work with structured data constantly:

sample = {"location": "Wellington", "pH": 7.2, "status": "clean"}

# Access values
sample["pH"]          # 7.2

# Useful methods
sample.keys()         # all keys
sample.values()       # all values
sample.items()        # key-value pairs

# Update
sample.update({"temperature": 18.5})

# Delete
del sample["status"]

# Remove and return last item
last = sample.popitem()

Sets — Unique Collections

Sets automatically remove duplicates. Defined with curly braces:

readings = {7.2, 6.8, 7.2, 7.5, 6.8}
# Result: {7.2, 6.8, 7.5}
  • remove() raises an error if the element doesn't exist
  • discard() silently does nothing if the element is missing
  • Use & for intersection (elements in both sets)

Essential Python Libraries

Python's power comes from its libraries. Install them with pip install:

# Import the entire library
import math

# Import a specific function
from math import sqrt

# Import with an alias
import math as m

# Import everything (use sparingly)
from math import *

Libraries You'll Use Most

LibraryPurpose
NumPyNumerical computing, arrays, matrix operations
PandasDataFrames, data manipulation, CSV handling
MatplotlibBasic plotting and visualisation
SeabornStatistical visualisation (built on Matplotlib)
Scikit-learnML algorithms, model training, evaluation
randomRandom number generation
osFile system operations (os.getcwd() for current directory)

Pro Tip

In Jupyter notebooks, you can run shell commands by prefixing with !:

!pip install pandas

This is handy for quick installs without leaving your notebook.

Subscribe to new posts

Get notified when I publish new learnings. No spam, unsubscribe anytime.