🔷 WEEK 1 Lesson 4
Title: Python for AI Systems — Writing Clean, ML-Ready Code
Lesson Objective
By the end of this lesson, learners will:
Refresh core Python skills needed for AI
Understand functions & modular coding
Write structured, reusable code
Understand NumPy fundamentals
Write ML-ready, clean engineering code
This lesson is about discipline.
AI engineers do not write messy notebooks.
They write structured systems.
1. Python Refresher — What Actually Matters for AI
You do not need all of Python.
You need:
Variables
Data types (int, float, string, bool)
Lists
Dictionaries
Loops
Conditionals
Functions
Imports
AI Engineering requires clarity, not clever tricks.
Example mindset:
Bad:
x=[1,2,3];y=[4,5,6];print([x[i]+y[i] for i in range(3)])
Engineer mindset:
def add_vectors(a, b):
result = []
for i in range(len(a)):
result.append(a[i] + b[i])
return result
Readable. Maintainable. Debuggable.
2. Functions & Modular Coding
Functions are critical in AI systems.
Why?
Because AI projects grow quickly.
Instead of writing everything in one file, engineers:
Separate data loading
Separate preprocessing
Separate model training
Separate evaluation
Example structure:
project/
│
├── data_loader.py
├── preprocess.py
├── train.py
├── evaluate.py
└── main.py
Each file has clear responsibility.
This is professional coding discipline.
3. Writing ML-Ready Code
ML-ready code is:
Clean
Reusable
Well-named
Structured
Commented where necessary
Example:
Bad variable name:
x = data[:,0]
Better:
feature_column = data[:, 0]
Engineers optimize for readability, not shortcuts.
4. Why NumPy Is Essential
NumPy is the backbone of ML computation in Python.
It allows:
Fast numerical operations
Vectorized computation
Matrix multiplication
Linear algebra operations
Without NumPy, ML would be too slow.
5. NumPy Basics (Core Concepts Only)
Creating Arrays:
import numpy as np
vector = np.array([1, 2, 3])
matrix = np.array([[1, 2], [3, 4]])
Shape:
vector.shape
matrix.shape
Shape tells you structure:
(rows, columns)
Understanding shape prevents many ML bugs.
6. Vectorized Operations
NumPy allows element-wise operations:
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
a + b
No loops required.
This is faster and cleaner.
7. Matrix Multiplication in NumPy
X = np.array([[1, 2],
[3, 4]])
weights = np.array([0.5, 1.0])
prediction = X @ weights
That single line:
X @ weights
Is what powers most ML models.
Understanding this is critical.
8. Engineering Discipline: Avoid Notebook Chaos
Common beginner mistake:
Everything in one Jupyter notebook.
Professional approach:
Prototype in notebook
Move stable code into .py files
Use functions
Create reusable pipelines
Your future self must understand your code.
Mini Practical Exercise
Create a Python script that:
Creates a NumPy matrix (5 rows, 3 features)
Creates a weight vector (3 values)
Computes predictions using matrix multiplication
Wrap computation inside a function.
Print shape of input and output.
This builds production habit early.
Week 1 – Lesson 4 Outcome
Students now:
✔ Refresh core Python skills
✔ Understand modular coding
✔ Understand clean engineering structure
✔ Understand NumPy arrays & shapes
✔ Perform matrix multiplication in code
✔ Write ML-ready structured scripts
Next:
🔷 Lesson 5 — Data Structures & Algorithms for AI
Then
🔷 Lesson 6 — Git & GitHub Workflow + Mini Engineering Setup Project.
Powered by Soft AI Africa | Training the Next Generation of AI Leaders in Africa.