Skip to chapter content
navan✳THE BUILD LABMACHINE LEARNING / GUIDE
0/28 read
CHAPTER 02 / 28 · FIRST PRINCIPLES

The Python you need.

Read names, lists, functions, imports, and method calls before meeting a model API.

BY NAVAN SAMILONG READTHEORY FIRST
01 / FROM THE BEGINNING

Start with the meaning.

Python source code is text that an interpreter executes. A line such as count = 3 assigns the number 3 to the name count. The equals sign here means “store this value under this name,” not “prove an equation.” A later line can read count. Names are case-sensitive, and the program normally moves from top to bottom. Quotation marks make text values called strings; 3 is a number, while '3' is text. This distinction matters because adding two numbers performs arithmetic, whereas joining text produces a longer string.

Lists are ordered collections written in square brackets. values = [2, 4, 6] has three elements. Python counts positions from zero, so values[0] is 2. A for loop visits elements one after another. Its indented block repeats for each element. Indentation is part of Python's syntax: lines at the same level belong to the same block. A function, defined with def, gives a block a name and can return a value to its caller. A function call uses parentheses to supply arguments.

02 / THE MODEL

How the idea works.

Machine learning examples also use imports. import math asks Python to make a module available under the name math; from statistics import mean brings one named function into the file. A module is a file or package of reusable code. Libraries such as NumPy, pandas, and scikit-learn are collections of modules installed separately from Python's standard library. You can understand a model without memorising every import, but you should know which name came from the library and which name your own code created.

The dot connects an object to one of its attributes or methods. In model.fit(X, y), fit is a method on model. Parentheses mean the method is called, and X and y are values passed in. A model object is created first; fitting changes its learned internal state. model.predict(new_X) later reads that state and returns predictions. The name X conventionally means the input feature table, and y means the target answers. They are only names, not special Python keywords.

A return value differs from printed output. print(mean(values)) displays the result; mean(values) itself produces a number that can be assigned or used in another expression. Most learning APIs return data rather than print it. Reading a code example therefore means tracing which lines create values, which lines change objects, and which lines merely display a result. Comments beginning with # explain intent to humans and are ignored by the interpreter.

03 / PYTHON, EXPLAINED

Follow the code.

python-for-machine-learning.py
from statistics import mean

measurements = [4, 6, 8]
average = mean(measurements)


def above_average(value, boundary):
    return value > boundary

for measurement in measurements:
    print(measurement, above_average(measurement, average))

The import gives us a function named mean. measurements is a list of three numbers. mean(measurements) returns 6, which is assigned to average. The def line defines a new function with two parameters: value and boundary. Its return expression compares them with > and produces True or False. Defining the function does not call it. The loop calls it once for each measurement, passing the current number and the average. The displayed results are False, False, and True.

This is not a learned model: the comparison rule was written directly. It shows the Python grammar later examples rely on. The function's parameter names exist only during a call; the outer names measurements and average remain available outside. If measurements were empty, mean would have no average to calculate. Inputs and their assumptions matter even for simple code, and they matter still more once a model learns from many rows.

04 / THE BOUNDARY

What the result does not say.

The cookbook uses scikit-learn to show complete examples. Those scripts use the same syntax introduced here: imports, lists or arrays, object creation, fit, predict, and display. Each recipe also explains what its library calls mean. You can read the course first without installing packages or running code.

Python's own guide in The Build Lab covers the language more broadly, including files and automation. This machine learning course repeats the essentials needed to read its examples so the learning path remains self-contained.

05 / KEEP THIS

One clear idea.

Read Python by following values through assignments, calls, returns, and object methods.

THE CODE IN CONTEXT

The ML cookbook

Complete, explained Python workflows sit alongside this reading course.

Browse the cookbook
Finished reading?
Built with curiosity. By Navan Sami. ↗Machine learning with Python · From examples to trustworthy models.